I have trouble resolving a recursion issue, how can I build a class which would behave like this:
- print the requested attributes from it:
o.foo.barwould print"o.foo.bar"when called. - same thing for a function:
o.foo.bar(True)->"o.foo.bar(True)". - this attribute call will return a value when called, for example "1".
Code would look like:
>>> o = MyClass()
>>> result = o.foo.bar
"o.foo.bar"
>>> print(result)
1
I've tried some things with __getattr__ but I cannot manage to handle recursion correctly, I cannot get when the end of the nested chain has been reached and then return the result accurately.
That's the closest I got:
class MyClass:
def __init__(self):
self.a = []
def __getattr__(self, name):
self.a.append(name)
print(self.a)
return self
o = MyClass()
result = o.foo.bar
PURPOSE
This is a simplified code to get rid of context complexity. I'm currently trying to wrap the JS API of Photoshop and this could help calling the software functions by writing same API functions in Python. The print would be replaced by a call to the PS Server and the result by the actual result of the call.
'o.foo.bar'to the Photoshop API the straightforward way instead of going through all this?