Python basic 1-class properties and instance properties

Source: Internet
Author: User
Tags instance method

The class property is 类对象 property, which is owned by all 类对象 of the 实例对象 in common, there is only one copy in memory, which is somewhat similar to the static member variables of classes in C + +. For common class properties, outside the class, you can pass 类对象 and 实例对象 access

Class properties
class people:    name = ‘Tom‘  #公有的类属性 __age = 12 #私有的类属性p = people()print p.name #正确print people.name #正确print p.__age #错误,不能在类外通过实例对象访问私有的类属性print people.__age #错误,不能在类外通过类对象访问私有的类属性
Instance Properties

Instance properties are not required to be defined in the class, such as:

class people:    name = ‘Tom‘p = people()p.age =12print p.name #正确print p.age #正确print people.name #正确print people.age #错误

After the class object people is instantiated outside the class, an instance object p is produced, and then P.age = 12 Adds an instance attribute of age to P, which is assigned a value of 12. This instance property is unique to the instance object P, noting that the class object people does not own it (so the age property cannot be accessed through the class object). Of course, you can also assign an age value when instantiating an object.

 class  People:name =  "Tom"  #__init__ () is a built-in construction method that automatically calls def __init__ ( Self,age): Self.age = Agep = People (12) print p.name  #正确 print p.age  #正确 print people.name  #正确 print People.age  #错误           

If you need to modify outside 类属性 of the class, you must do 类对象 so by referencing and then modifying it. If a reference is made through an instance object, it will produce a name that 实例属性 is modified in such a way that it will 实例属性 not be affected 类属性 , and then if the property of that name is referenced through the instance object, the instance property will force the class property to be masked, that is 实例属性 , unless it is deleted 实例属性 .

class people:    country = ‘china‘print people.countryp = people()print p.countryp.country = ‘japan‘ print p.country #实例属性会屏蔽掉同名的类属性print people.countrydel p.country #删除实例属性print p.country

Summarize

For class and instance properties, if a property is referenced in a class method, the property must be a class property, and if a property is referenced in an instance method (unchanged), and a class property of the same name exists, the instance property masks the class property if the instance object has an instance property of the attribute, that is, the instance property is referenced. If the instance object does not have an instance property of that name, then the Class property is referenced, if a property is changed in an instance method, and if there is a class property of the same name, if the instance object has an instance property of the attribute, then the instance property is modified, and if the instance object does not have an instance property of that name, an instance property To modify a class property, if it is outside the class, it can be modified by the class object, and if inside the class, it is only modified in the class method.

Python basic 1-class properties and 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.