Say we have a class:
class Foo (object):
... def __init__(self,d):
... self.d=d
... def return_d(self):
... return self.d
... and a dict:
d={'k1':1,'k2':2}
... and an instance:
inst=Foo(d)
Is there a way to dynamically add attributes to return_d so:
inst.return_d.k1 would return 1?
inst.return_d()['k1']do? The.operator does an attribute lookup, so whateverreturn_dreturns, should support attribute lookups, perhaps via the__getattr__hook. Note that you expectreturn_dto be an attribute, but you defined it as a method, so the example you state would not work on that account alone.