super, decorators and gettattribute

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michele Simionato

    #16
    Re: super, decorators and gettattribute

    On Jan 14, 11:47 pm, Richard Szopa <ryszard.sz...@ gmail.comwrote:
    Could you tell me what are the pros and cons of the two approaches
    (i.e. writing a decorator function and a decorator descriptor class)?
    I prefer to use a class for introspection sake, since
    there is no way to get information about an inner function
    in a closure, whereas your users can introspect classes
    pretty well.
    super_object = super(self.__cl ass__, self)
    Notice that using super(self.__cl ass__, self) is a common
    mistake: the pitfalls with inheritance were discussed
    very recently in this same newsgroup, you should be
    able to find the post. What you need is

    super(class_whe re_the_method_i s_defined, self)

    which is in general different from

    super(self.__cl ass__, self)

    There is no clean way to determine the current class
    in Python < 3.0 (Python 3.0 does it automatically);
    if you want to see a hackish way involving
    bytecode tricks see


    (and notice that this is really not recommended).

    Michele Simionato

    Comment

    • Rhamphoryncus

      #17
      Re: super, decorators and gettattribute

      On Jan 13, 5:51 am, Richard Szopa <ryszard.sz...@ gmail.comwrote:
      On Jan 13, 8:59 am, Marc 'BlackJack' Rintsch <bj_...@gmx.net wrote:
      >
      On Sat, 12 Jan 2008 14:23:52 -0800, Richard Szopa wrote:
      However, I am very surprised to learn that
      >
      super_object.__ getattr__(name) (*args, **kwargs)
      >
      getattr(super_o bject, name)(*args, **kwargs)
      >
      are not equivalent. This is quite odd, at least when with len()
      and .__len__, str() and .__str__. Do you maybe know what's the
      rationale behind not following that convention by getattr?
      >
      I think you are confusing `__getattr__` and `__getattribute __` here!
      `getattr()` maps to `__getattr__()` , it's `__getattribute __` that's
      different.
      >
      Well, in my code calling super_object.__ getattr__(name) (*args,
      **kwargs) and getattr(super_o bject, name)(*args, **kwargs) gives
      *different* effects (namely, the latter works, while the former
      doesn't). That kinda suggests that they don't map to each other :-).
      And that makes me feel confused.
      Don't think of them as mappings. Think of them as a way for a class
      to hook into getattr's protocol, conveniently named similar to getattr
      (__getattr__ and __getattribute_ _). getattr() may call several
      methods, no methods at all, change the arguments, etc. Although len()
      may seem simple, many others are not so simple.

      --
      Adam Olsen, aka Rhamphoryncus

      Comment

      • Helmut Jarausch

        #18
        Re: super, decorators and gettattribute

        Michele Simionato wrote:
        I really need to publish this one day or another, since these
        questions
        about super keeps coming out:
        >
        http://www.phyast.pitt.edu/~micheles/python/super.html
        Unfortunately the links [2], [3] and [4] are not given,

        Helmut.


        --
        Helmut Jarausch

        Lehrstuhl fuer Numerische Mathematik
        RWTH - Aachen University
        D 52056 Aachen, Germany

        Comment

        • Richard Szopa

          #19
          Re: super, decorators and gettattribute

          On Jan 15, 8:06 pm, Helmut Jarausch <jarau...@skyne t.bewrote:
          Unfortunately the links [2], [3] and [4] are not given,
          Luckily, there's Google :)

          [2] Article about MRO: http://www.python.org/download/releases/2.3/mro/
          [3] Descriptor HowTo: http://users.rcn.com/python/download/Descriptor.htm
          [4] Articles about metaclasses (second one relevant to super):
          * http://www.ibm.com/developerworks/li.../l-pymeta.html,
          * http://www-128.ibm.com/developerwork...GX03&S_CMP=ART,
          * http://www.ibm.com/developerworks/li...GX03&S_CMP=ART

          Of course, it would be very nice if the article was updated to include
          the links.

          HTH,

          -- Richard

          Comment

          Working...