The difference and relation between Python CLS and self; Class A: the difference and relation between a and a() and a =a(); And the difference and relation between instance space and class space
編輯:Python
cls And self The difference and connection
Conclusion :A For the class , Address and cls equally ,a For instanced objects , Address and self equally A() It can also trigger for instantiating objects __init_ Method
(__init_ Magic method through class addition () Trigger )
A And A() And a =A() The difference and connection
""" Instance object call Class call Access instance properties Access class properties Example method 1 0 1 1 Quasi square Law 1 1 / 1 Static methods 1 1 / 0 Common method 0 1 / 0 https://blog.csdn.net/weixin_44259638/article/details/121318407 """
class A(object):
nation = "china" # Class properties
def __init__(self): # Magic methods Encapsulate attributes on instantiated objects
self.province = " sichuan " # Instance attributes
# 1. Example method The instance
def sky(self):
name = "phil"
# return print(self.province)
# print(id(self))
print(" I am an instance method that returns ")
# 2. Static methods No parameters required Both class and instance methods can call
@staticmethod
def land():
age = 18
# return print(nation)
# return print(self.province)
print(" I am a static method that returns ")
# 3. Class method Both class and instance methods can call
@classmethod
def person(cls):
cls.sex = "male" # This is a class attribute
return print(cls.nation)
# return print(id(cls))
# print(" I am a class method that returns ")
# 4. Common method quti
def normal():
print(" I am a normal method that returns ")
# print()1
# Instantiation method Ordinary methods cannot call When accessing properties, first access , First, access the instance property and the class property
a = A()
# a.sky() # result : I am an instance method that returns
# a.land() # result : I am a static method that returns
a.person() # result : I am a class method that returns
# a.normal() # Operation error reporting Because common methods require self Parameters
# Class call Instance methods are not callable
# A.sky() # Error running instance method
# A.land() # result : I am a static method that returns
# A.person() # result : I am a class method that returns
# A.normal() # result : I am a normal method that returns
# print(A.province) Cannot access instance properties
# A() Ordinary methods cannot call
# A().person() # 1
# A().land() # 1
# A().sky() # 1
# A().normal() # 0
# print(A().province) # sichuan You can access instance properties You can think of it as an instantiation
# a = A()
# print(id(a))
# a.sky()
# a.person()
# print(a.__dict__)
The difference and relation between instance space and class space
class Test:
a = 1
def test(self):
print(self.a) # Could not find... In the properties of the instance space a, Just go to class space to find , Found for 1
self.a += 2 # Create in instance space a attribute , The initial value is 3
print(self.a) # Instance space found a The attribute is 3
print(Test.a) # 1 Class properties a by 1
b = Test()
b.test() # 1 3
print(b.a) # 3 Instance space found a The attribute is 3
print(Test.a) # Space like a still 1