Python class variables and object variable declaration resolution

Source: Internet
Author: User

Python's classes are like C + +, and there are two types of variables, class variables, and object variables! The former is owned by the class and is shared by all objects, which are unique to each object. Here, I would like to discuss their declaration method mainly.

The first thing to say is the object variable:

As long as it is declared in the statement block of the class , and there is no "self." The variables of the prefix are class variables, and the class variables are shared by all objects.

Note that the Scarlet Letter section, if declared in the statement block of the method of the class, is the local variable! For example, the following:

1 #!/usr/bin/env python2 #-*-Coding:utf-8-*-3 #function:use the class var4 5 classPerson :6Cvar = 17     defSayhi (self):8Fvar = 19 Ten PrintPerson.cvar One PrintPerson.fvar

That Cvar is the Python class variable, and that Fvar is the method Sayhi () in the local variables, the 11th statement there will be an error!

Let's talk about the declaration method of the object variable:

Declared in the statement block of the class and in the statement block of its method with "self." The variable at the beginning is an object variable, unique to the object!

For example, the following:

1 #!/usr/bin/env python2 #-*-Coding:utf-8-*-3 #Function:use the object var4 5 classPerson :6     defHavename (self):7Self.name ='Michael'8     defSayname (self):9         PrintSelf.nameTen  One defMain (): Ap =Person () -  - P.havename () the P.sayname () -  -Main ()

Here an object variable is declared in the Havename () method, and then called in the Sayname () method. Then the main program will be output!

However, it is recommended to declare the object variable in the __init__ () method, because the object is called when it is created, otherwise, such as the above example, if I first call Sayname (), then there will be an error, saying that the object instance has no Name this property!

Finally, the point is that Python does not have the private public these keywords to indicate the class's variable or method access rights, but can be by adding "__" in front of the variable or method to indicate that the member is a class private, cannot be called externally, such as the following example:

1 #!/usr/bin/env python2 #-*-Coding:utf-8-*-3 #Function:use the private var and func4 5 classPerson :6     __count= 0#This variable is a private data member and can only be accessed by the method of the class, which belongs to the class.7     defGet (self):8         returnPerson.__count9     def __pri(self):Ten         Print 'Yes' One  Ap =Person () - PrintP.get () -  theP.__pri() - PrintP.__count

For example, the class variable __count is a class-private, can only be called by the function members of the class (13 rows), and calls outside the class (16 lines) is wrong! And that function member __pri () is also private, calling directly outside of the class (15 lines), is also wrong!

Python class variables and object variable declaration resolution

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.