another super wart

Christian Tanzer tanzer at swing.co.at
Thu Mar 13 18:14:35 CET 2003


mis6 at pitt.edu (Michele Simionato) wrote:

(snip)
> 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.

Now I'm not convinced. You didn't show any code that did or didn't
work so I assumed you had stumbled over the same thing as I had:

>>> class Meta(type) :
...     def new(cls, a) :
...         print "Meta.new", cls, a
...     def init(cls, a) :
...         print "Meta.init", cls, a
...     def __new__(meta, name, bases, dict) :
...         result = super(Meta, meta).__new__(meta, name, bases, dict)
...         print result.new
...         result.new(name)
...         return result
...     def __init__(cls, name, bases, dict) :
...         super(Meta, cls).__init__(name, bases, dict)
...         cls.init(name)
...
>>> class Class(object) :
...     __metaclass__ = Meta
...     def new(self, b, c) :
...         print "Class.new", self, b, c
...     def init(self, b, c):
...         print "Class.init", self, b, c
...     def __new__(self, b, c) :
...         result = super(Class, self).__new__(self)
...         result.new(1, 2)
...         return result
...     def __init__(self, b, c) :
...         super(Class, super).__init__()
...         self.init(3, 4)
...
<unbound method Class.new>
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 9, in __new__
TypeError: unbound method new() must be called with Class instance as first argument (got str instance instead)
>>>

Compare that to:

>>> class Meta(type) :
...     def m_new(cls, a) :
...         print "Meta.new", cls, a
...     def m_init(cls, a) :
...         print "Meta.init", cls, a
...     def __new__(meta, name, bases, dict) :
...         result = super(Meta, meta).__new__(meta, name, bases, dict)
...         print result.new
...         result.m_new(name)
...         return result
...     def __init__(cls, name, bases, dict) :
...         super(Meta, cls).__init__(name, bases, dict)
...         cls.m_init(name)
...
>>> class Class(object) :
...     __metaclass__ = Meta
...     def new(self, b, c) :
...         print "Class.new", self, b, c
...     def init(self, b, c):
...         print "Class.init", self, b, c
...     def __new__(self, b, c) :
...         result = super(Class, self).__new__(self)
...         result.new(1, 2)
...         return result
...     def __init__(self, b, c) :
...         super(Class, self).__init__()
...         self.init(3, 4)
...
<unbound method Class.new>
Meta.new <class '__main__.Class'> Class
Meta.init <class '__main__.Class'> Class
>>> instance = Class(42,137)
Class.new <__main__.Class object at 0x824de64> 1 2
Class.init <__main__.Class object at 0x824de64> 3 4
>>>

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

Have fun,

-- 
Christian Tanzer                                         tanzer at swing.co.at
Glasauergasse 32                                       Tel: +43 1 876 62 36
A-1130 Vienna, Austria                                 Fax: +43 1 877 66 92






More information about the Python-list mailing list