Type emulation issues with new style classes
Chris
feb04.20.netman at spamgourmet.com
Sat Feb 28 13:08:02 EST 2004
I tried a couple variation of that, and __getattr__, when defined in a
metaclass is never called when accessing an attribute on an instance of a
class derived from that metaclass.
Here is some testing I did:
class Zmeta(type):
def __getattribute__(*args):
raise Exception('called __getattribute__ with %s' % str(args))
def __getattr__(*args):
raise Exception('called __getattr__ with %s' % str(args))
Z = Zmeta('Z', (), {'value': 42})
z = Z()
>>> int(z)
TypeError: int() argument must be a string or a number
>>> z.__int__
AttributeError: 'Z' object has no attribute '__int__'
>>> Z.__int__
Exception: called __getattribute__ with (<class '__main__.Z'>, '__int__')
It appears (and is confirmed in: Metaclass Programming in Python Pt. 2
http://www-106.ibm.com/developerworks/library/l-pymeta2/?ca=dnt-434) that
metaclass attributes are available to instances (classes) but not instances
of instances.
Chris
"Michael Hudson" wrote:
>
> > Is there any way to make the class Z behave the same way as class Y?
>
> You need a custom metaclass for Z, and implement __getattr__ there.
>
> HTH.
>
> Cheers,
> mwh
>
> --
> Lisp does badly because we refuse to lie. When people ask us if
> we can solve insoluble problems we say that we can't, and because
> they expect us to lie to them, they find some other language
> where the truth is less respected. -- Tim Bradshaw, comp.lang.lisp
More information about the Python-list
mailing list