Python class variable and member variable usage example tutorial, python instance tutorial

Source: Internet
Author: User

Python class variable and member variable usage example tutorial, python instance tutorial

This article provides examples to illustrate the usage of class variables and member variables in python, 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.


How does a python class member become an instance member?

The instance can directly use the member variables of the class. Note that the relationship between class variables and instance variables is similar to the principle of variable parameters and immutable parameters. You can refer to the following example (python 2.7) to help you:
# Encoding: UTF-8class A (object): x = 100y = [,] def _ init _ (self ): passif _ name _ = "_ main _": a = A (). x = "test" print. xprint. xa. x = 'xyz'. x = "hello" print. xprint. xprint. yprint. ya. y. append ("x") print. yprint. y output result:




Python member variable value change

Class:
M = 5
N = 6
P = 7
A = ()
Paralist = {"m": 6, "n": 7, "p": 8}

For k in paralist. keys ():
A. _ dict _ [k] = paralist [k]

Print a. m, a. n, a. p

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.