__dict__(字典的另一种用法)


class Foo():
def __init__(self):
self.name=None
self.age=19
self.addr='上海'

@property
def dict(self):
#传过来的self是对象,Foo,直接__dict__就是打印出字典的形式
return self.__dict__



obj=Foo()
# print(obj.__dict__)
# print(obj.dict)
class Foo2():
def fun(self):
obj.name='yunxin'
obj.age=20
obj.addr='江西'
return obj.dict
##在下面已经全部重新构造了这个字典里面的值
#直接调用上面的对方法,就可以通过点的方式来直接对字典的值做操作,不需要是dic['name']='yunxin'来进行赋值了


obj1=Foo2()
print(obj1.fun())
'''{'name': 'yunxin', 'age': 20, 'addr': '江西'}'''
原文地址:https://www.cnblogs.com/yunxintryyoubest/p/9902995.html