I have a class that implements virtual attributes using __getattr__ . Attributes can be expensive, for example. fulfilling the request. Now I use a library that checks if my object has an attribute before it gets it.
As a result, the request is executed twice instead of one. Of course, it makes sense to actually execute __getattr__ to really know if the attribute exists.
class C(object): def __getattr__(self, name): print "I was accessed" return 'ok' c = C() hasattr(c, 'hello')
Is there any way to prevent this?
If Python supports __hasattr__ , then I could just check if the request exists, on the contrary, it actually starts it.
I can create a cache, but it is heavy, as the request may have parameters. Of course, the server can cache the requests themselves and minimize the problem, but it is still difficult if the requests return a large amount of data.
Any ideas?
python getattr
koriander
source share