I want to be able to do the following
JavaScript
x
12
12
1
class Parent:
2
def __init__(self):
3
pass
4
def Create(self):
5
return 'Password'
6
7
class Child(Parent):
8
def __init__(self):
9
self.Create()
10
def Create(self):
11
return 'The '+self.Create #I want to return 'The Password'
12
I would like to get a parent function from a child class in a function that is overriding it. I am not sure how to do this.
This is a little hard to explain, comment if you are having trouble understanding.
Edit:
Thanks for the answers everyone, I almost thought that this was impossible.
Advertisement
Answer
The super()
function is meant for cases like this. However, it only works on “new-style” classes, so you’ll need to modify your definition of Parent
to inherit from object
as well (you should always be using “new-style” classes, anyway).
JavaScript
1
15
15
1
class Parent(object):
2
def __init__(self):
3
pass
4
def Create(self):
5
return 'Password'
6
7
class Child(Parent):
8
def __init__(self):
9
self.Create()
10
def Create(self):
11
return 'The ' + super(Child, self).Create()
12
13
14
print Child().Create() # prints "The Password"
15