Saturday, 24 August 2013

Confusion on using super with new style classes

Confusion on using super with new style classes

I have this:
#! /usr/bin/env python
class myclass1(object):
def __new__(cls,arg):
print cls, arg, "in new"
ss = super(object,cls)
print ss, type(ss)
ss.__new__(cls,arg)
# super(object,cls).__new__(cls,arg)
# return object.__new__(cls,arg)
def __init__(self,arg):
self.arg = arg + 1
print self, self.arg, "in init"
if __name__ == '__main__':
m = myclass1(56)
It gives an error:
$ ./newtest.py
<class '__main__.myclass1'> 56 in new
<super: <class 'object'>, <myclass1 object>> <type 'super'>
Traceback (most recent call last):
File "./newtest.py", line 23, in <module>
m = myclass1(56)
File "./newtest.py", line 9, in __new__
ss.__new__(cls,arg)
TypeError: super.__new__(myclass1): myclass1 is not a subtype of super
The error is valid. I get that. However I am now confused as to what the
documentation is saying on this page for __new__:
http://docs.python.org/2.6/reference/datamodel.html#object.__new__
Question: What am I doing wrong as per the documentation above. Where is
the gap in my understanding of the documention ?

No comments:

Post a Comment