官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 永年县| 阳山县| 武定县| 泰宁县| 建湖县| 锡林郭勒盟| 新邵县| 平和县| 泾川县| 新密市| 磴口县| 县级市| 榆树市| 河源市| 图木舒克市| 三明市| 曲阜市| 台北县| 和龙市| 古丈县| 徐水县| 阳江市| 德阳市| 布尔津县| 富阳市| 荆州市| 古蔺县| 宝鸡市| 雷山县| 西吉县| 阿克苏市| 饶阳县| 高雄县| 定日县| 柯坪县| 息烽县| 达拉特旗| 虎林市| 武强县| 威远县| 双牌县|