The Association of Python class attributes with instance properties

Source: Internet
Author: User

A class property is a variable belonging to a class, like a static member variable of a class in C + +, you simply define the property in the domain of all methods, that is, the class property, but usually immediately following the class name, the Class property is common to all instances, and you can invoke the Class property by using the class name. property

>>> class A:count = 0; #这就是类属性 def __init__ (self): A.count + = 1 #每次调用该方法 count auto-increment 1 def output: print (Self.cou  NT) >>> a1 = A () >>> a1.output () 1 >>> a.count = 0 >>> a.count 0 >>> A1.output () (Why would it be 0, which will be explained in detail later) 0

Instance properties are attributes that belong to the instance itself, and you can add new instance properties to any method, even outside of the class, and Python creates the property and assigns it when the instance property is first used

>>> class a:      def __init__ (self):           self.num = 1   #添加实例属性num        def again (self,name):           self.name  = name  #添加实例属性name                >>> a1 = a ()   >>> a1.num  1   >>> a1.name  #这时实例  a1  has no instance properties  name  Traceback  (most  Recent call last):    file  "<pyshell#38>",  line 1, in  <module>      a1.name  #这时实例  a1  has no instance attribute  name   AttributeError:  ' A '  object has no attribute  ' name '   > >> a1.again (' Jane ')  >>> a1.name  #现在有了 ...   ' Jane '   >>> a1.call  =  ' 123456 '   #添加a1的实例属性  call  >>> a1.call   ' 123456 '

keep looking at the following example:

[Python] view plaincopy>>> class a:      count  = 0      def __init__ (self):           a.count += 1      def output (self):           print (Self.count)                >>> a1 = a ()   >>>  a2 = a ()   >>> a3 = a ()   >>> a.count The class property of the  # a count is now 3  3  >>> a.count = 2  #更改A的类属性为2   >>> a1.count,a2.count, a3.count, A.count  #A的所有实例的count也同样改变     (2, 2, 2, 2)   >>> a1.count = 5  #通过A的一个实例a1更改count   >>> a1.count, a2.count, a3.count, a.count  #只有a1的count发生改变    (5, 2, 2, &NBSP;2)   >>> A.count = 4  #再次更改A的类属性为4   >>>  a1.count, a2.count, a3.count, a.count   #这时a1的count还是保持为5    (5, 4,  4,&NBSP;4)

Explain why it's 0?

Look at the code first:

Class Base:count = 0 def __init__ (self): Base.count + = 1 def output (self): print (self.count) A1 = b ASE () a2 = base () a3 = base () print (base.count) # 3a1.output () # 3 Here The instance properties are still common to the class reference a Count object Base.count + = 4a1.output () # 3+4 Print (Base.count) # 3print (a1.count) # 3 Similarly, a common reference to a Count object Base.count = 0 when the class attribute count is changed, the instance property count also changes to print (A1.count) # 0
class base:    count =  0    def __init__ (self):         Base.count += 1    def output (self):         print (Self.count) a1 = base () a2 = base () a3 = base () print (Base.count)  # 3a1.count = 9   This instance property is detached from the class attribute base.count += 4  Therefore, the change in the class attribute count does not affect the instance properties Countprint (Base.count)  # 7print (a1.count)  # 9print (base.count)   # 3a1.count = 9base.count += 4print (Base.count)  # 7a1.output ()  #  9 

As we can see from the above example, the Class property is common to all instances and classes, and the class name can be changed by the classes property, and the class properties of all instances change, but the class property is changed by the instance name. Class property, which becomes an instance property, without affecting the class properties of the other instance, later by Class name. Class property to change the class property, nor does it affect this property of the instance, because it becomes an instance property.



The Association of Python class attributes with instance properties

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.