Python对象的属性像字典一样地被修改

    科技2025-02-10  15

    参考链接: Python官方文档:模块 参考链接: Python官方文档:object.dict 参考链接: Python官方文档:object.str(self) 参考链接: Python官方文档:object.getitem(self, key) 参考链接: Python官方文档:object.setitem(self, key, value)

    主要使用了魔法方法: def __str__(self): def __getitem__(self,key): def __setitem__(self,key,value): 使用了一个特殊属性: object.__dict__

    官方文档上更详细的说明:

    Special Attributes The implementation adds a few special read-only attributes to several object types, where they are relevant. Some of these are not reported by the dir() built-in function. object.__dict__ A dictionary or other mapping object used to store an object’s (writable) attributes. 特殊属性: 语言实现为部分对象类型添加了一些特殊的只读属性,它们具有各自的作用。 其中一些并不会被 dir() 内置函数所列出。 object.__dict__ 一个字典或其他类型的映射对象,用于存储对象的(可写)属性。 # 参考链接:https://docs.python.org/zh-cn/3.8/library/stdtypes.html#object.__dict__ A special attribute of every module is __dict__. This is the dictionary containing the module’s symbol table. Modifying this dictionary will actually change the module’s symbol table, but direct assignment to the __dict__ attribute is not possible (you can write m.__dict__['a'] = 1, which defines m.a to be 1, but you can’t write m.__dict__ = {}). Modifying __dict__ directly is not recommended. 每个模块都有一个特殊属性 __dict__。 这是包含模块的符号表的字典。 修改此字典将实际改变模块的符号表, 但是无法直接对 __dict__ 赋值 (你可以写 m.__dict__['a'] = 1,这会将 m.a 定义为 1,但是你不能写 m.__dict__ = {})。 不建议直接修改 __dict__。 # 参考链接:https://docs.python.org/zh-cn/3.8/library/stdtypes.html#modules

    代码:

    class Person(object): def __init__(self,age,name): print("方法:__init__(self,age,name)正在被调用中...") self.age = age self.name = name def __str__(self): print("方法:__str__(self)正在被调用中...") return 'name:{} age:{}'.format(self.name,self.age) def __getitem__(self,key): print('方法:__getitem__(self,key)正在被调用中...') return self.__dict__[key] def __setitem__(self,key,value): print('方法:__setitem__(self,key,value)正在被调用中...') self.__dict__[key] = value if __name__ == '__main__': #pass #f() p1 = Person(10,'zhangsan') print("1111111111111111111111111111111111111111111111") print(p1) print("2222222222222222222222222222222222222222222222") print(type(p1.__dict__)) #p1.__dict__会把这个对象的属性以及值以键值对的形式转化成字典 print("3333333333333333333333333333333333333333333333") p1['name'] = 'tom' #以字典的形式给属性赋值会调用__setitem__方法 print("4444444444444444444444444444444444444444444444") print(p1.name) print("5555555555555555555555555555555555555555555555") print(p1['name']) #以字典的形式获取值会调用__setitem__方法 print("6666666666666666666666666666666666666666666666") str(p1) print("7777777777777777777777777777777777777777777777") print(type([])) print("8888888888888888888888888888888888888888888888") print(type(())) print("9999999999999999999999999999999999999999999999") print(type({})) print("0000000000000000000000000000000000000000000000")

    控制台输出:

    Windows PowerShell 版权所有 (C) Microsoft Corporation。保留所有权利。 尝试新的跨平台 PowerShell https://aka.ms/pscore6 PS C:\Users\chenxuqi\Desktop\新建文件夹> & 'D:\Python\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2020.9.114305\pythonFiles\lib\python\debugpy\launcher' '54339' '--' 'c:\Users\chenxuqi\Desktop\新建文件夹\test.py' 方法:__init__(self,age,name)正在被调用中... 1111111111111111111111111111111111111111111111 方法:__str__(self)正在被调用中... name:zhangsan age:10 2222222222222222222222222222222222222222222222 <class 'dict'> 3333333333333333333333333333333333333333333333 方法:__setitem__(self,key,value)正在被调用中... 4444444444444444444444444444444444444444444444 tom 5555555555555555555555555555555555555555555555 方法:__getitem__(self,key)正在被调用中... tom 6666666666666666666666666666666666666666666666 方法:__str__(self)正在被调用中... 7777777777777777777777777777777777777777777777 <class 'list'> 8888888888888888888888888888888888888888888888 <class 'tuple'> 9999999999999999999999999999999999999999999999 <class 'dict'> 0000000000000000000000000000000000000000000000 PS C:\Users\chenxuqi\Desktop\新建文件夹>
    Processed: 0.016, SQL: 8