__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': '江西'}'''


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM