-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?

5
  • 1
    You can format lines of code in your post by selecting the block of code and clicking the button with curly braces. Commented Aug 31, 2017 at 19:27
  • 2
    You seem confused about how classes work. Why would you call super - or print - outside of a method? Commented Aug 31, 2017 at 19:28
  • Also, is the i key on your keyboard broken? There are seven missing is in that code. Commented Aug 31, 2017 at 19:30
  • Yes daniel I just started learning python. So in order to use super it should always be under some method Commented Aug 31, 2017 at 19:30
  • yes the 'i' key is not working. :( Commented Aug 31, 2017 at 19:31

2 Answers 2

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 ;)

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.