Python class variable and object variable declaration parsing, python variable

Source: Internet
Author: User

Python class variable and object variable declaration parsing, python variable

Similar to C ++, Python classes also have two types of variables: class variables and object variables! The former is owned by the class and shared by all objects, and the latter is exclusive to each object. Here I mainly want to discuss their declaration methods.

 

The first thing to do is the object variable:

As long as the variable is declared in the statement block of the class and without the "self." prefix is a class variable, and the class variable is shared by all objects.

Note the red part. If it is declared in the statement block of the class method, it is a local variable! For example:

 1 #!/usr/bin/env python 2 # -* - coding: UTF-8 -* - 3 #Function: Use the class var 4  5 class Person: 6     cvar = 1 7     def sayHi(self): 8       fvar = 1 9 10 print Person.cvar11 print Person.fvar

That cvar is a variable of the Python class, and that fvar is a local variable in the method sayHi (). An error will be reported in the 11th statements!

 

Next we will discuss how to declare object variables:

The variables starting with "self." declared in the statement block of the class and the statement block of the method are all object variables, which are unique to the object!

For example:

 1 #!/usr/bin/env python 2 # -* - coding: UTF-8 -* - 3 #Function: Use the object var 4  5 class Person: 6     def haveName(self): 7       self.name = 'Michael' 8     def sayName(self): 9       print self.name10 11 def main():12     p = Person()13 14     p.haveName()15     p.sayName()16 17 main()

An object variable is declared in the haveName () method and then called in the sayName () method. Then the output will be in the main program!

However, we recommend that you declare the object variables in the _ init _ () method, because the method will be called when the object is created. Otherwise, for example, in the example above, if I call sayName () First, an error occurs, indicating that the object instance does not have the name attribute!

 

The last thing I want to talk about is that Python does not have the private public keywords to indicate the access permissions of class variables or methods, however, you can add "_" before a variable or method to indicate that the Member is private to the class and cannot be called externally. For example:

1 #! /Usr/bin/env python 2 #-*-coding: UTF-8-*-3 # Function: Use the private var and func 4 5 class Person: 6 _ count = 0 # This variable is a private data member and can only be accessed by class methods. It belongs to Class 7 def get (self): 8 return Person. _ count 9 def _ pri (self): 10 print 'yes' 11 12 p = Person () 13 print p. get () 14 15 p. _ pri () 16 print p. _ count

For example, the class Variable _ count here is private to the class. It can only be called by class function members (13 rows), but it is wrong to call outside the class (16 rows! The function member _ pri () is also private. It is also wrong to call the function directly outside the class (15 rows!

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.