SoFunction
Updated on 2024-11-12

Python Class Properties and Instance Properties Usage Analysis

This article example analyzes Python class properties and instance properties usage. Shared for your reference. The specifics are as follows:

Class properties: class name. Attribute name

Instance properties: instance . Attribute name

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

1. How class attributes are molded the same, once a class attribute is given, all instances will take that value.
2. The value of this attribute can vary across instances.
3. The attribute of an instance is not given in the display ground. you can display the value of this attribute. however. it is not inside the namespace.

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

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

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

I hope that what I have described in this article will help you in your Python programming.