Python class variable and member variable usage example tutorial

Source: Internet
Author: User
This article mainly introduces the usage of class variables and member variables in python, which must be well mastered in Python programming, if you need it, you can refer to the examples in this article to explain the usage of python class variables and member variables, which has some reference value for Python program design. Share it with you for your reference. The details are as follows:

Let's take a look at the following code:

class TestClass(object):  val1 = 100    def __init__(self):    self.val2 = 200    def fcn(self,val = 400):    val3 = 300    self.val4 = val    self.val5 = 500  if __name__ == '__main__':  inst = TestClass()     print TestClass.val1  print inst.val1  print inst.val2  print inst.val3  print inst.val4    print inst.val5

Here, val1 is a class variable, which can be called directly by the class name or an object;
Val2 is a member variable and can be called by class objects. here we can see that the member variables must be given in the form of self, because the meaning of self is to represent the instance object;
Val3 is not a member variable, but a local variable in the fcn function;
Both val4 and val5 are not member variables. although given by self., they are not initialized in the constructor.

Let's take a look at the following code (# is followed by the running result ):

inst1 = TestClass()inst2 = TestClass()print TestClass.val1 # 100print inst1.val1   # 100inst1.val1 = 1000  print inst1.val1   # 1000print TestClass.val1 # 100TestClass.val1 =2000 print inst1.val1   # 1000print TestClass.val1 # 2000print inst2.val1   # 2000   inst3 = TestClass() print inst3.val1   # 2000

It can be found that the class variables of python are different from the static variables of C ++, and are not shared by all objects of the class. The class itself has its own class variables (stored in memory). When an object of the TestClass class is constructed, the current class variables will be copied to this object, the value of the current class variable is, and the value of the class variable copied from this object is. besides, modifying the class variable through the object does not affect the value of the class variable of other objects, because everyone has their own copies, it will not affect the value of the class variable owned by the class itself; only the class itself can change the value of the class variable owned by the class itself.

I hope the examples described in this article will help you understand and understand the usage of class variables and member variables in Python.

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.