python中__dict__的作用

python中__dict__的作用

   运维菜鸟     2021年2月27日 10:34     1179    

class People(object):
    var1 =
1
   
var2 = 2
   
var3 = 3
   
def __init__(self,name , age , weight):
       
self.name = name
       
self.age = age
       
self.weight = weight

   
def __lt__(self, other):
       
print("compare.....")
       
return self.age<other.age

   
def __str__(self):
       
return 'name:'+ self.name+'' + 'age:'+ str(self.age)+'' + 'weight:' + str(self.weight) + ''

   
def test_fun(self):
       
print("test unit")

zhang = People(
'zhang' , 23 , 64)

print(zhang.__dict__)
print(People.__dict__)

运行结果:

{'name': 'zhang', 'age': 23, 'weight': 64}

{'__module__': '__main__', 'var1': 1, 'var2': 2, 'var3': 3, '__init__': <function People.__init__ at 0x000002CC61951280>, '__lt__': <function People.__lt__ at 0x000002CC61951790>, '__str__': <function People.__str__ at 0x000002CC61951820>, 'test_fun': <function People.test_fun at 0x000002CC619518B0>, '__dict__': <attribute '__dict__' of 'People' objects>, '__weakref__': <attribute '__weakref__' of 'People' objects>, '__doc__': None}

 

在运行结果中可以看到实例的__dict__和对象的__dict__中包含不同的属性和方法,并以字典的形式进行存储的,其中属性名作为键,而值作为该键对应的值。

对象的__dict____init__()中包含的属性,不包含任何方法。

 

而类中的对象包含类属性,类函数,普通的方法等

 

一些内置的类型是没有__dict__属性的,比如int,list,dict等。

 

class People(object):
    var1 =
1
   
var2 = 2
   
var3 = 3
   
def __init__(self,name , age , weight):
       
self.name = name
       
self.age = age
       
self.weight = weight

   
def __lt__(self, other):
       
print("compare.....")
       
return self.age<other.age

   
def __str__(self):
       
return 'name:'+ self.name+'' + 'age:'+ str(self.age)+'' + 'weight:' + str(self.weight) + ''

   
def test_fun(self):
       
print("test unit")

class Student(People):
   
def __init__(self,name , age , weight,score):
       
super(Student, self).__init__(name , age , weight)
       
self.score = score
   
def print_score(self):
       
print(self.name + ":" + str(self.score))


zhang = People(
'zhang' , 23 , 64)
print(zhang.__dict__)
print(People.__dict__)

zhao = Student(
'zhao' , 24,65 , 80)
zhao.print_score()
print(zhao.__dict__)
print(Student.__dict__)


运行结果:

{'name': 'zhang', 'age': 23, 'weight': 64}

{'__module__': '__main__', 'var1': 1, 'var2': 2, 'var3': 3, '__init__': <function People.__init__ at 0x0000025C843D1280>, '__lt__': <function People.__lt__ at 0x0000025C843D1790>, '__str__': <function People.__str__ at 0x0000025C843D1820>, 'test_fun': <function People.test_fun at 0x0000025C843D18B0>, '__dict__': <attribute '__dict__' of 'People' objects>, '__weakref__': <attribute '__weakref__' of 'People' objects>, '__doc__': None}

 

{'name': 'zhao', 'age': 24, 'weight': 65, 'score': 80}

{'__module__': '__main__', '__init__': <function Student.__init__ at 0x0000025C843D1940>, 'print_score': <function Student.print_score at 0x0000025C843D19D0>, '__doc__': None}

子类和父类都有各自的__dict__


可以对__dict__进行增删改查。

修改

zhao.__dict__.update(name="xiaozhao" , age="34",weight="66" , score=100)



文章评论

0

其他文章