another super wart

Michele Simionato mis6 at pitt.edu
Thu Mar 13 10:20:12 EST 2003


tanzer at swing.co.at (Christian Tanzer) wrote in message news:<mailman.1047541607.23655.python-list at python.org>...
> mis6 at pitt.edu (Michele Simionato) wrote:
> 
> > I have found a very subtle problem (bug?) with super used in conjunctio
>  n
> > with   new  . It does not do what I expect.
>  (snip)
> > How is it possible?? It seems that   new   is very special and behaves
> > in the opposite way of new! (  init   instead would work as expected).
> 
> It isn't a bug, it is a feature. `super` doesn't really have anything
> to do with it.
> 
> For new-style classes, python handles magic methods differently than
> normal methods. For normal methods, python uses the standard attribute
> lookup rules, i.e., it first looks in the instance dictionary,
> followed by the class dictionary, followed by the bases of the class.
> 
> For magic methods, python *ignores* the instance dictionary and goes
> directly to the class dictionary. It must do so, because otherwise
> metaclasses would not work.
> 
> Alex Martelli recently posted a much longer explanation
> (Message-id: <b42ev802l51 at enews4.newsguy.com>).
> 
> > Discovering this very subtle inconsistency (which I would call a bug
> > in the implementation of super) made me loose an afternoon :-(
> 
> I also lost a few hours (and gained a few grey hairs) with this
> feature. Unfortunately, the only thing to do about it is improve the
> documentation.
> 
> My personal conclusion was to prefix all metaclass methods with `m `
> (much as I detest such naming conventions in general). Using
> `classmethod` instead of defining methods in the metaclass also might
> help to keep sanity <wink>.


Not convinced. 

The problem is only with __new__, not with the other special methods: as
I said in my originale posting, it would work with __init__. Try it!
I also checked other special methods, it works as for normal methods.
I see in that thread that Samuele Pedroni replies to Alex Martelli
statement

>
> In Python 2.2 and later (excepting, for backwards compatibility,
> 'classic classes') an operation always looks up the special methods
> of interest on the CLASS (aka type) of the object being operated on.
>

by saying

> this is true (only exception is partially the static special method
>__new__) 

I guess he discovered the same trouble before me. 

I am going to explore this further in the next days. 


      Michele




More information about the Python-list mailing list