Python class attributes and instance attributes usage analysis-Python tutorial

Source: Internet
Author: User
This article mainly introduces the usage of Python class attributes and instance attributes, and analyzes the functions, definitions, and usage skills of Python class attributes and instance attributes, for more information about Python class attributes and instance attributes, see the following example. Share it with you for your reference. The details are as follows:

Class property: class name. property name

Instance property: instance. property name

>>> class test():...  ver=1... >>> a=test()>>> test.x=8>>> a.__dict__{}>>> a.x8>>> a.x=9>>> a.__dict__{'x': 9}

1. how is the model of the class property the same? once the class property is given, all instances will get this value.
2. the attribute values of each instance can be changed.
3. if the attribute of an instance is not displayed, a. x can display the value of this attribute, but it is not in the namespace.

In order to add it to the namespace, it must be explicitly assigned a value.

class Instant1(object): count=0 def __init__(self):  Instant1.count=Instant1.count+1  print "created instant" def howmany(self):  print Instant1.count  print self.countclass Instant2(object): count=0 def __init__(self):  print self.count  #print Instant2.count  self.count=self.count+1  #Instant2.count=Instant2.count+1  print "created instant" def howmany(self):  print self.count  print Instant2.count

I hope this article will help you with Python programming.

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.