python之类属性和实例对象属性

一曲冷凌霜 提交于 2019-12-09 20:25:58

python中的实例对象属性(Instance Attributes)就是跟这个对象绑定的简单的数据而已。实例对象属性一般在__init__函数中初始化,但是在其它方法中也是可以随意添加新的属性。

class Foo(object):
    def __init__(self, name):
        super(Foo, self).__init__()
        self.name = name    // 实例对象属性

    def fun(self):
        self.sugar = "whatever"    // 给实例添加属性,但并不提倡这么做,所有的实例属性最好在__init__中给出
需要注意的是,self指代是实例化后对象。


类属性可以理解为跟类绑定的数据,跟实例无关,但是我们可以通过实例化对象引用类属性。python核心编程中有一句描述我认为描述不当。(When an instance is created, Python provides a default, read-only instance attribute which is an alias to the class attribute.)实际上,当实例化对象的时候,新的实例得到是类属行的一个引用,这个引用不能说是可读的,可读不可读都是针对所引用的这个属性是否可变,数字,字符串是不可边的数据类型,而list或者自定义对象等都是可变类型。为了更好的表达,我用一个例子来说明:

In [5]: class Foo:
   ...:     count = 0
   ...:     lst = [1, 6, 3, 2, 9]
   ...:     def __init__(self, name="foo"):
   ...:         self.name = name
   ...:         
   ...:
In [10]: Foo.count
Out[10]: 0

In [11]: Foo.lst
Out[11]: [1, 6, 3, 2, 9]

In [12]: id(Foo.count), id(Foo.lst)    // 类属性的地址
Out[12]: (141233524, 144539980)

In [13]: f1 = Foo()

In [14]: id(f1.count), id(f1.lst)    // 实例对象属性的地址
Out[14]: (141233524, 144539980)    // 新创建的对象的属性跟类属性执行同一地址的数据

In [15]: f1.lst
Out[15]: [1, 6, 3, 2, 9]

In [16]: f1.lst.sort()    // 这里对lst属性做原地排序,

In [17]: f1.lst    // 排序后的结果
Out[17]: [1, 2, 3, 6, 9]

In [18]: Foo.lst    // 通过实例更改了类的可变属性,说明引用不是只读的
Out[18]: [1, 2, 3, 6, 9]

In [19]: id(f1.lst), id(Foo.lst)  
Out[19]: (144539980, 144539980)    // 通过实例更改类的可变属性后,类属性和实例对象属性地址不变
In [20]: f1.lst = []

In [21]: f1.count += 100

In [23]: id(f1.count),id(Foo.count),id(f1.lst), id(Foo.lst)
Out[23]: (141234300, 141233524, 144488652, 144539980)    // 任何针对实例对象的赋值语句都会接触类属性的版定,而新建一个新的同名实例对象属性


参考: core python programing, dive into python

这里是相关摘要:

Class Attributes and Intance Attributes

class attributes are simply data values associated with a class and not any particular instances like instance attributes are. Such values are also referred to as static members because their values stay constant, even if a class is invoked due to instantiation multiple times. No matter what, static members maintain their values independent of instances unless explicitly changed. Comparing instance attributes to class attributes is almost like the comparison between automatic and static variables, if you are familiar with these concepts from other languages.

There are a few aspects of class attributes versus instance attributes that should be brought to light. The first is that you can access a class attribute with either the class or an instance, provided that the instance does not have an attribute with the same name.

Any type of assignment of a local attribute will result in the creation and assignment of an instance attribute, just like a regular Python variable. If a class attribute exists with the same name, it is overridden in the instance. (compare with list.sort() or list.reverse() function).

Binding and Method Invocation.

Now we need to readdress the Python concept of binding, which is associated only with method invocation. We will first review the facts about methods. First, a method is simply a function defined as part of a class. This means that methods are class attributes (not instance attributes). Second, methods can be invoked only when there is an instance of the class in which the method was defined. When there is an instance present, the method is considered bound. Without an instance, a method is considered unbound. And third, the first argument in any method definition is the variable self, which represents
the instance object which invokes the method.












易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!