Attributes and methods are divided into instance properties/Methods and Class Properties/methods:
One: Properties:
As far as possible, the attributes that need to be passed in as instance properties are used as properties of the same class. Instance properties are initialized once for each class created, the instance properties of different instances may be different, and the class properties of the different instances are the same. thus reducing memory.
1: Instance Properties:
Preferably in __init__ (self,...) Initialization in
Internal calls need to be self-added.
External call with Instancename.propertyname
2: Class Properties:
Initialize outside __init__ ()
Called internally with the ClassName. Class Property name
External can be used ClassName. Class property names can also be invoked with the InstanceName. Class property name.
3: Private Property:
1): Single underline _ Start: Just tell someone that this is a private property and that the external access can still be changed
2): Double underline __: External cannot be visited or changed by Instancename.propertyname
Actually convert it to _classname__propertyname
Two: Methods
1: Common class Method:
def fun_name (self,...):
Pass
External Use instance invocation
2: Static method: @staticmethod
Cannot access instance Properties!!! Parameter cannot be passed into self!!!
Methods that are related to classes but do not depend on classes and instances!!
3: Class method: @classmethod
Cannot access instance Properties!!! Parameters must be passed in cls!!!
You must pass in the CLS parameter (that is, the-----difference that represents such an object------self represents the instance object), and use this call Swimming class property: CLS. Class Property Name
* Both static and class methods can be invoked through classes or instances. Two of its features are not able to invoke instance properties
E.G1:
Class A:
member = "This is a test."
def __init__ (self):
Pass
@classmethod
def Print1 (cls):
print "Print 1:", cls.member
def Print2 (self):
print "Print 2:", self.member
@classmethod
def Print3 (paratest):
Print "Print 3:", Paratest.member
@staticmethod
def print4 ():
print "Hello"
Summary: Class properties and class methods are intrinsic to the class and properties, not because of the different instances of the change, write their purpose is to reduce the number of instances created by the memory space, speed up the operation.
Python: Class Properties, instance properties, private properties and static methods, class methods, instance methods