- Daniel Arbuckle's Mastering Python
- Daniel Arbuckle
- 223字
- 2021-07-02 21:09:32
Attributes
We discussed a little while ago that we could add attributes to function objects, which is often handy. We could do something similar with classes, with one big difference- attributes that we add to functions are only visible to the code that has access to that function object, which usually doesn't include the code of the function itself, but attributes that we add to class objects are visible to any code that has access to the class object or to an object of the type described by the class.
This means that if we add an attribute to a class, the functions defined in that class will be able to access that attribute through the self parameter, as shown in the following code example:

We need to be careful when adding attributes to classes because if we accidentally overwrite one of the class' attributes, we could break the class.
We have a greater ability to manipulate classes than functions. So, we need to use that ability more thoughtfully. Also, notice that, in this example, one of the attributes we added to the class is a function, which then proceeded to work exactly as if it had been defined as a part of the class from the beginning.
Next, let's take a short tour of some of the highlights of Python's standard library.
- Koa開發(fā):入門、進(jìn)階與實戰(zhàn)
- Julia Cookbook
- Effective Python Penetration Testing
- Apache Mesos Essentials
- ASP.NET程序設(shè)計教程
- Hands-On Reinforcement Learning with Python
- Android開發(fā)案例教程與項目實戰(zhàn)(在線實驗+在線自測)
- Visual Basic程序設(shè)計教程
- Red Hat Enterprise Linux Troubleshooting Guide
- Beginning C++ Game Programming
- R語言:邁向大數(shù)據(jù)之路(加強(qiáng)版)
- Java高并發(fā)編程詳解:深入理解并發(fā)核心庫
- JavaScript前端開發(fā)基礎(chǔ)教程
- 詩意的邊緣
- Magento 2 Developer's Guide