-1

I was trying my hands on super function, below is the code i was executing.

class scene(object):
    def enter(self):
        print "a vllan s n your way. what you'll do?"

class centralcorrdor(scene):
    print "startng pont of the game."
    super(centralcorrdor,self).enter()


a = centralcorrdor()

however this gives error.

class centralcorrdor(scene):
File "game.py", line 8, in centralcorrdor
super(centralcorrdor,self).enter()

NameError: name 'centralcorrdor' is not defined

And this does not.

class scene(object):
    def enter(self):
        print "a vllan s n your way. what you'll do?"

class centralcorrdor(scene):
    #print "startng pont of the game."
    def func(self):
        super(centralcorrdor,self).enter()
    #scene.enter()

a = centralcorrdor()
a.func()

Can someone tell why? Is it that super has be called from inside a method in child class?

Vladyslav
  • 2,018
  • 4
  • 18
  • 44
ps3790
  • 1
  • 4

2 Answers2

0

You must use the super within a method. For more information you can consult: python-programming or using-super-with-a-class-method I hope this help you ;)

Dayana
  • 1,500
  • 1
  • 16
  • 29
0

super is actually a proxy to your base classes. There is class proxy (defined in a static or a class method) and instance proxy (defined in an instance method).

Your super(centralcorrdor,self).enter() statement pass self as the object argument, and if you check enter signature is self. So, you have to call it on an instance object, not in a class method.

In general, you would call super(class, object) most of the time.

Chen A.
  • 10,140
  • 3
  • 42
  • 61