Type emulation issues with new style classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Hudson

    #16
    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

    Comment

    • Aahz

      #17
      Re: Type emulation issues with new style classes

      In article <95aa1afa.04022 90216.18ee8000@ posting.google. com>,
      Michele Simionato <michele.simion ato@poste.it> wrote:[color=blue]
      >
      >You may also look at this bug report:
      >
      >http://sourceforge.net/tracker/?grou...ail&aid=789262
      >
      >The special lookup of special attributes has bitten at least three or four
      >person on this mailing list in the last year or so: I maintain that
      >it is a documentation bug.[/color]

      Just as a reminder, here's a nifty way of reporting SF URLs that makes
      life easier:

      --
      Aahz (aahz@pythoncra ft.com) <*> http://www.pythoncraft.com/

      "Do not taunt happy fun for loops. Do not change lists you are looping over."
      --Remco Gerlich, comp.lang.pytho n

      Comment

      Working...