__getattr__ on non-instantiated class

bruno at modulix onurb at xiludom.gro
Wed May 3 12:41:19 EDT 2006


Larry Bates wrote:
> Fredp wrote:
> 
>>Hi
>>I was wondering if it is possible to have the various magic methods,
>>mainly __getattr__ and __setattr__, and @property attributes called
>>when accessing the attribute of a non-intantiated class.
>>
>>Imagin something like this:
>>#####
>>class MyClass:
>>     @property
>>     def prop(self):
>>           print "Accessed"
>>           return "ABCD"
>>
>>print MyClass.prop
>>#####
>>having it printing:
>>#####
>>Accessed
>>ABCD
>>#####
>>
>>Thanks very much
>>
> 
> Looks like you want Python to execute a method on an uninstantiated
> class. 

s/uninstantiated//

Python's classes are objects, and as such can have attributes and
methods (read about staticmethod or classmethod).

> I can't imagine how you would use such a thing.

I do - that's something I do quite frequently.

> 
> This produces the output you want:
> 
> m=MyClass()
> print m.prop()

But this is not what the OP asked for !-)


-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list