- 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.
- DB2 V9權威指南
- Vue.js設計與實現
- Angular UI Development with PrimeNG
- PHP+MySQL網站開發技術項目式教程(第2版)
- Learn WebAssembly
- Python程序設計案例教程
- OpenStack Orchestration
- Python Web數據分析可視化:基于Django框架的開發實戰
- C#實踐教程(第2版)
- HTML 5與CSS 3權威指南(第3版·上冊)
- Modern C++ Programming Cookbook
- Java Web從入門到精通(第2版)
- Ext JS 4 Plugin and Extension Development
- Python Programming for Arduino
- Mastering Apache Camel