程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

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 )
class A(object):
def sky(self):
print(id(self)) # 2839707071368
@classmethod
def person(cls):
return print(id(cls)) # 2839705905128
a = A()
print(id(a)) # 2839707071368
a.sky()
a.person()
print(id(A)) # 2839705905128
cls And self Are to open up the space for instantiating objects
class A:
def __init__(self):
pass
def fun(self):
self.a = 1
print(self.__dict__)
# print(cls.__dict__)
print(A.__dict__)
def fuc(self):
self.b = 2
print(self.a)
print(self.b)
print(self.__dict__)
# print(cls.__dict__)
print(A.__dict__)
def func(cls):
cls.c=3
print(cls.c)
# print(self.__dict__)
print(cls.__dict__)
print(A.__dict__)
a=A()
a.fun()
a.fuc()
a.func()

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

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved