descriptors and old-style classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adrien Di Mascio

    descriptors and old-style classes

    Hi,

    I've recently discovered that descriptors can be used with old-style
    classes if they don't define a __set__ method (and this would be why
    class and static methods are usable with old-style classes).
    I understand why the __set__ method is not called with old-style
    classes, but I don't understand why __get__ *is* called.
    I thought the transformation from :
    a.x
    into :
    type(a).__dict_ _['x'].__get__(a, type(a))
    was done in object.__getatt ribute__ (or type.__getattri bute__), but then
    this does not explain why this also seems to work with old-style
    classes.

    Could someboby help me here ??

    Cheers,
    Adrien.

    --
    Adrien Di Mascio
    LOGILAB, Paris (France).
    http://www.logilab.com http://www.logilab.fr http://www.logilab.org

  • John Roth

    #2
    Re: descriptors and old-style classes


    "Adrien Di Mascio" <adim@logilab.f r> wrote in message
    news:slrnc1d88e .qr.adim@lacert a.logilab.fr...[color=blue]
    > Hi,
    >
    > I've recently discovered that descriptors can be used with old-style
    > classes if they don't define a __set__ method (and this would be why
    > class and static methods are usable with old-style classes).
    > I understand why the __set__ method is not called with old-style
    > classes, but I don't understand why __get__ *is* called.
    > I thought the transformation from :
    > a.x
    > into :
    > type(a).__dict_ _['x'].__get__(a, type(a))
    > was done in object.__getatt ribute__ (or type.__getattri bute__), but then
    > this does not explain why this also seems to work with old-style
    > classes.
    >
    > Could someboby help me here ??[/color]

    At a rough guess, the search logic is in __getattribute_ _, but the
    actual invocation is probably in the bytecode somewhere.
    __getattribute_ _, after all, is supposed to return the attribute
    requested, not to invoke it.

    So on an old style class, the standard search will find the
    descriptor object just fine, then the bytecode will detect it
    has a __get__ method and invoke it.

    John Roth[color=blue]
    >
    > Cheers,
    > Adrien.
    >
    > --
    > Adrien Di Mascio
    > LOGILAB, Paris (France).
    > http://www.logilab.com http://www.logilab.fr http://www.logilab.org
    >[/color]


    Comment

    Working...