Python Super()

Hey guys I am messing with Python and I am wondering why the super() keyword isnt working in this short script or how to use the super() word?

`class Person:	
	def __init__(self,agef,namef):
		self.age=agef
		self.name=namef
	def getAge(self):
		return self.age
	def setAge(self,num):
		self.age=num
class Bob(Person):
	def __init__(self,agef,namef):
		super(Bob,self).__init__(agef,namef)
		#Person.__init__(self,agef,namef)
				
	

p = Bob(15,"bob",92)
p.setAge(29)
print p.getAge()

	
`

Hi Chiefshane,

I don't deal with classes a lot, so might have to dig deeper. However, I think super is only used to override the child class, in case you have the same method name in the parent and child. So in case of confusion, you say super and the parent's method is used.

This code works for me. The moment class Bob is intialized it has all the methods of the parent, "person", so there is no need to use super in your example.

class Person:	
	def __init__(self,agef,namef):
		self.age=agef
		self.name=namef
	def getAge(self):
		return self.age
	def setAge(self,num):
		self.age=num

class Bob(Person):
	def __init__(self,agef,namef):
		self.age=agef
		self.name=namef

				
# I think super is only used to override stuff 

p = Bob(15,"bob")
p.setAge(29)
print p.getAge()

More information is available here, and here.

  1. Your indentations are all screwed up.
  2. You call the bob constructor incorrectly, it takes 2 variables not 3.
  3. Person needs to be defined as an object (i.e. class Person(object): )

In [4]: p = Bob(92, "bob")
In [5]: p.getAge()
Out[5]: 92
In [6]: p.setAge(29)
In [6]: p.getAge()
Out[6]: 29

class Person(object):
    def __init__(self,agef,namef):
	self.age=agef
	self.name=namef
    def getAge(self):
	return self.age
    def setAge(self,num):
	self.age=num
class Bob(Person):
    def __init__(self,agef,namef):
        super(Bob,self).__init__(agef,namef)