Python Macros

Jacek Generowicz jacek.generowicz at cern.ch
Wed Oct 6 17:12:01 EDT 2004


On 6 Oct 2004, at 19:09, Arich Chanachai wrote:

>> You mean some refinement of this sort of thing:
>>
>>    class X(object):
>>           a = "lives in X"
>>           def __getattr__(self, name):
>>            return getattr(U,name)
>>       class U(object):
>>           b = "lives in Y"
>>       x = X()
>>    print x.a
>>    print x.b
>>
>> ?
>>
> Sure, but what if I dynamically (at runtime) extend X to include 
> variable b.  Will it then print X.b or still U.b?

Did you try it?

 >>> print x.b
lives in Y
 >>> X.b = "was added to X at runtime"
 >>> print x.b
was added to X at runtime
 >>>

__getattr__ only gets invoked if the attribute is not found in the 
instance, class and superclass dictionaries.




More information about the Python-list mailing list