标签:如何 dict python 函数 属性 object __weak pre code
# 先定义类
class LuffyStudent():
school = "luffycity" # 数据属性
def learn(self): # 函数属性
print("is learning...")
def sleep(self): # 函数属性
print("is sleeping...")
# 查看类的名称空间
print(LuffyStudent.__dict__)
# {'__module__': '__main__', 'school': 'luffycity', 'learn': <function LuffyStudent.learn at 0x00000000025B8B70>, 'sleep': <function LuffyStudent.sleep at 0x00000000025B8BF8>, '__dict__': <attribute '__dict__' of 'LuffyStudent' objects>, '__weakref__': <attribute '__weakref__' of 'LuffyStudent' objects>, '__doc__': None}
print(LuffyStudent.__dict__["school"]) # luffycity
print(LuffyStudent.__dict__["learn"]) # <function LuffyStudent.learn at 0x00000000025B8B70>
# 查
print(LuffyStudent.school) # luffycity
print(LuffyStudent.learn) # <function LuffyStudent.learn at 0x0000000002158AE8>
# 增
LuffyStudent.country = "China"
print(LuffyStudent.country) # China
# 删
del LuffyStudent.country
print(LuffyStudent.__dict__)
# {'__module__': '__main__', 'school': 'luffycity', 'learn': <function LuffyStudent.learn at 0x0000000002158AE8>, 'sleep': <function LuffyStudent.sleep at 0x0000000002158B70>, '__dict__': <attribute '__dict__' of 'LuffyStudent' objects>, '__weakref__': <attribute '__weakref__' of 'LuffyStudent' objects>, '__doc__': None}
# 改
LuffyStudent.school = "foguang University"
print(LuffyStudent.__dict__)
# {'__module__': '__main__', 'school': 'foguang University', 'learn': <function LuffyStudent.learn at 0x0000000002158AE8>, 'sleep': <function LuffyStudent.sleep at 0x0000000002158B70>, '__dict__': <attribute '__dict__' of 'LuffyStudent' objects>, '__weakref__': <attribute '__weakref__' of 'LuffyStudent' objects>, '__doc__': None}
未完待续。。。。。。
标签:如何 dict python 函数 属性 object __weak pre code
原文地址:https://www.cnblogs.com/friday69/p/9347710.html