python之類屬性和實例對象屬性

python中的實例對象屬性(Instance Attributes)就是跟這個對象綁定的簡單的數據而已。實例對象屬性通常在__init__函數中初始化,可是在其它方法中也是能夠隨意添加新的屬性。 python

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 less

這裏是相關摘要: ide

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. spa

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. code

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. ip

相關文章
相關標籤/搜索