Re: Type emulation issues with new style classes
"Chris" <feb04.20.netma n@spamgourmet.c om> writes:
[color=blue]
> 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('call ed __getattribute_ _ with %s' % str(args))
> def __getattr__(*ar gs):
> raise Exception('call ed __getattr__ with %s' % str(args))
>
> Z = Zmeta('Z', (), {'value': 42})
> z = Z()
>[color=green][color=darkred]
> >>> int(z)[/color][/color]
> TypeError: int() argument must be a string or a number[color=green][color=darkred]
> >>> z.__int__[/color][/color]
> AttributeError: 'Z' object has no attribute '__int__'[color=green][color=darkred]
> >>> Z.__int__[/color][/color]
> 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/developerwork...a2/?ca=dnt-434) that
> metaclass attributes are available to instances (classes) but not instances
> of instances.[/color]
Argh. Yes, now I think about the implementation, this isn't that
surprising. OTOH, one shouldn't have to know Objects/typeobject.c as
well as I do to avoid being surprised by Python...
I've added some more detailed comments to the bug report Michele
mentioned, if you're curious.
Cheers,
mwh
--
SCSI is not magic. There are fundamental technical reasons why it
is necessary to sacrifice a young goat to your SCSI chain now and
then. -- John Woods
"Chris" <feb04.20.netma n@spamgourmet.c om> writes:
[color=blue]
> 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('call ed __getattribute_ _ with %s' % str(args))
> def __getattr__(*ar gs):
> raise Exception('call ed __getattr__ with %s' % str(args))
>
> Z = Zmeta('Z', (), {'value': 42})
> z = Z()
>[color=green][color=darkred]
> >>> int(z)[/color][/color]
> TypeError: int() argument must be a string or a number[color=green][color=darkred]
> >>> z.__int__[/color][/color]
> AttributeError: 'Z' object has no attribute '__int__'[color=green][color=darkred]
> >>> Z.__int__[/color][/color]
> 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/developerwork...a2/?ca=dnt-434) that
> metaclass attributes are available to instances (classes) but not instances
> of instances.[/color]
Argh. Yes, now I think about the implementation, this isn't that
surprising. OTOH, one shouldn't have to know Objects/typeobject.c as
well as I do to avoid being surprised by Python...
I've added some more detailed comments to the bug report Michele
mentioned, if you're curious.
Cheers,
mwh
--
SCSI is not magic. There are fundamental technical reasons why it
is necessary to sacrifice a young goat to your SCSI chain now and
then. -- John Woods
Comment