我有一堂课,里面有一个字典。我想通过直接“ instance.key”而不是“ instance.d [key]”来访问字典中的变量。该怎么做?
class A:
def __init__(self):
self.a = 1
self.b = 2
self.fields = {'c': 3, 'd': 4}
def main():
a = A()
print(a.a) # 1
print(a.b) # 2
print(a.c) # currently raise Exception, I want it to print 3
print(a.d) # currently raise Exception, I want it to print 4
if __name__ == '__main__':
main()
答案 0 :(得分:6)
您可以覆盖object.__getattr__(self, name)
方法(可以定义此类方法以自定义类实例的属性访问的含义):
class A:
def __init__(self):
self.a = 1
self.b = 2
self.fields = {'c': 3, 'd': 4}
def __getattr__(self, item):
return self.__dict__.get(item) or self.__dict__['fields'].get(item)
a = A()
print(a.a) # 1
print(a.b) # 2
print(a.c) # 3
print(a.d) # 4
参考链接:__getattr__
答案 1 :(得分:1)
您还可以修改__dict__
:
class A:
def __init__(self):
self.a = 1
self.b = 2
self.fields = {'c': 3, 'd': 4}
self.__dict__ = {**self.__dict__, **self.fields}
现在:
def main():
a = A()
print(a.a)
print(a.b)
print(a.c)
print(a.d)
if __name__ == '__main__':
main()
礼物:
1
2
3
4
答案 2 :(得分:0)
按照@RomanPerekhrest的建议执行__getattr__
。您可能遇到的唯一问题是您的实例属性将覆盖您的字典条目。如果这不是预期的行为,则应实施__getattribute__
而不是__getattr__
类似这样的东西:
class A:
def __init__(self):
self.a = 1
self.b = 2
self.e = 5
self.fields = {'c': 3, 'd': 4, 'e': 6}
def __getattribute__(self, key):
fields = super().__getattribute__('fields')
if key == 'fields':
return fields
try:
return fields[key]
except KeyError:
return super().__getattribute__(key)
a = A()
print(a.a) # 1
print(a.b) # 2
print(a.c) # 3
print(a.d) # 4
print(a.e) # 6 (Note that it returns the entry of the dict rather that the instance attribute)
print(a.f) # Raises exception (not in the instance attributes nor in the dict)