python之__dict__方法使用
python版本:3.6本文章仅供学习记录用,如有侵权,告知删除。1、类的__dict__属性和类对象的__dict__属性结论:a、类的静态函数、类函数、普通函数、全局变量以及一些内置的属性都是放在类__dict__里的b、对象的__dict__中存储了一些self.xxx的一些东西实例:class TestName:a = 2b = 2def __init__(self,c):self.a =
python版本:3.6 本文章仅供学习记录用,如有侵权,告知删除。
1、类的__dict__属性和类对象的__dict__属性
结论:
a、类的静态函数、类函数、普通函数、全局变量以及一些内置的属性都是放在类__dict__里的
b、对象的__dict__中存储了一些self.xxx的一些东西
实例:
class TestName:
a = 2
b = 2
def __init__(self,c):
self.a = 0
self.b = 1
self.c = c
def test1(self):
print("a normal func")
@staticmethod
def static_test(self):
print("a static class")
@classmethod
def class_test(self):
print("a class func")
o = TestName(2)
print(TestName.__dict__)
print(o.__dict__)
结果:
{'__module__': '__main__', 'a': 2, 'b': 2, '__init__': <function TestName.__init__ at 0x000001EFA81DD268>, 'test1': <function TestName.test1 at 0x000001EFA81DD1E0>, 'static_test': <staticmethod object at 0x000001EFA801B390>, 'class_test': <classmethod object at 0x000001EFA801B320>, '__dict__': <attribute '__dict__' of 'TestName' objects>, '__weakref__': <attribute '__weakref__' of 'TestName' objects>, '__doc__': None}
{'a': 0, 'b': 1, 'c': 2}
2、一些内置的数据类型是没有__dict__属性的,如:int, list, dict等这些常用的数据类型是没有__dict__属性的。
实例:
num = 1
list1 = []
dict1 = {}
print(num.__dict__)
print(list1.__dict__)
print(dict1.__dict__)
结果:
print(num.__dict__)
AttributeError: 'int' object has no attribute '__dict__'
print(list1.__dict__)
AttributeError: 'list' object has no attribute '__dict__'
print(dict1.__dict__)
AttributeError: 'dict' object has no attribute '__dict__'
3、继承中的__dict__属性,子类有自己的__dict__, 父类也有自己的__dict__,父类的全局变量和函数放在父类的dict中,子类的放在子类dict中。
a)继承类和被继承类的类变量、函数名都放在自己的__dict__中
b)实例变量的__dict__中,父类的对象和子类的对象中,dict__是公用的
结论:
1) 内置的数据类型没有__dict__属性
2) 每个类有自己的__dict__属性,就算存着继承关系,父类的__dict 并不会影响子类的__dict__
3) 对象也有自己的__dict__属性, 存储self.xxx 信息,父子类对象公用__dict__
实例:
class Person:
a = 1
b = 2
def __init__(self):
self.a = 3
self.b = 4
def per_test(self):
print("per_test")
class Student(Person):
c = 10
d = 20
def __init__(self):
super(Student, self).__init__()
def stu_test(self):
print("stu_test func")
def per_test(self):
print("stu_per_test func")
p = Person()
s = Student()
print(Person.__dict__)
print(Student.__dict__)
print(p.__dict__)
print(s.__dict__)
结果:
{'__module__': '__main__', 'a': 1, 'b': 2, '__init__': <function Person.__init__ at 0x0000022E0355D400>, 'per_test': <function Person.per_test at 0x0000022E0355D488>, '__dict__': <attribute '__dict__' of 'Person' objects>, '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__doc__': None}
{'__module__': '__main__', 'c': 10, 'd': 20, '__init__': <function Student.__init__ at 0x0000022E0355D510>, 'stu_test': <function Student.stu_test at 0x0000022E0355D598>, 'per_test': <function Student.per_test at 0x0000022E0355D620>, '__doc__': None}
{'a': 3, 'b': 4}
{'a': 3, 'b': 4}
4、利用__dict__可以给运行中的对象添加新的属性
实例(接上面例子中的代码):
s = Student()
s.__dict__["age"] = 10
print(s.__dict__)
结果:
{'a': 3, 'b': 4, 'age': 10} # 在上面的例子中,只有a,b两个属性,利用__dict__添加属性后,新增了一个age属性。
更多推荐
所有评论(0)