python - super() fails with error: TypeError "argument 1 must be type, not classobj" -


i error can't figure out. clue wrong sample code?

class b:     def meth(self, arg):         print arg  class c(b):     def meth(self, arg):         super(c, self).meth(arg)  print c().meth(1) 

i got sample test code of 'super' built-in method. class "c"

here error:

traceback (most recent call last):   file "./test.py", line 10, in ?     print c().meth(1)   file "./test.py", line 8, in meth     super(c, self).meth(arg) typeerror: super() argument 1 must type, not classobj 

fyi, here help(super) python itself:

help on class super in module __builtin__:  class super(object)  |  super(type) -> unbound super object  |  super(type, obj) -> bound super object; requires isinstance(obj, type)  |  super(type, type2) -> bound super object; requires issubclass(type2, type)  |  typical use call cooperative superclass method:  |  class c(b):  |      def meth(self, arg):  |          super(c, self).meth(arg)  | 

your problem class b not declared "new-style" class. change so:

class b(object): 

and work.

super() , subclass/superclass stuff works new-style classes. recommend in habit of typing (object) on class definition make sure new-style class.

old-style classes (also known "classic" classes) of type classobj; new-style classes of type type. why got error message saw:

typeerror: super() argument 1 must type, not classobj

try see yourself:

class oldstyle:     pass  class newstyle(object):     pass  print type(oldstyle)  # prints: <type 'classobj'>  print type(newstyle) # prints <type 'type'> 

note in python 3.x, classes new-style. can still use syntax old-style classes new-style class. so, in python 3.x won't have problem.


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -