__getattribute__ for class object

S

Sylvain Ferriol

hello
when i define __getattribute__ in a class, it is for the class instances
but if i want to have a __getattribute__ for class attributes

how can i do that ?

sylvain
 
P

Paolino

Sylvain said:
hello
when i define __getattribute__ in a class, it is for the class instances
but if i want to have a __getattribute__ for class attributes

how can i do that ?

Skating on thin ice eh.Read something on metaclasses.


class Meta(type):
def __getattribute__(klass,attr):
value=type.__getattribute__(klass,attr)
print attr,'==',value
return value

class Foo(object):
__metaclass__=Meta
a=2

Foo.a

Paolino
 
D

Dan

but if i want to have a __getattribute__ for class attributes
Read something on metaclasses.

Depending on what you want to do, it might be better to use properties
instead:

class Meta(type):
x = property(lambda klass: 'Called for '+str(klass))

class Foo(object):
__metaclass__=Meta

print Foo.x
 
S

Steven Bethard

Dan said:
Depending on what you want to do, it might be better to use properties
instead:

class Meta(type):
x = property(lambda klass: 'Called for '+str(klass))

class Foo(object):
__metaclass__=Meta

Also worth noting that you can inline the metaclass if you don't need it
anywhere else, e.g.:

class Foo(object):
class __metaclass__(type):
x = property(lambda klass: 'Called for '+str(klass))

STeVe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top