2

I am using inheritance. In one of my methods I would like to use both the parent attribute and the over-riden child attribute together. Something like;

class Parent(object):
 att = 'parent'
 def my_meth(self):
 return super().att + self.att
class Child(Parent):
 att = 'child'
print(Child().my_meth())

Which would print;

parentchild

However the above code gives the error;

'super' object has no attribute 'options'

Is this possible?

asked Dec 6, 2018 at 5:35
1
  • Did you tried with return super().__thisclass__.att + self.att? Commented Dec 6, 2018 at 5:47

2 Answers 2

2

A way I can think of accessing a static attribute of the parent class that gets overridden by the child is to directly refer to the parent class itself in the method:

class Parent(object):
 att = 'parent'
 def my_meth(self):
 return Parent.att + self.att
class Child(Parent):
 att = 'child'
print(Child().my_meth()) # parentchild
answered Dec 6, 2018 at 5:49
Sign up to request clarification or add additional context in comments.

Comments

1

As per the python document the super keyword Return a proxy object that delegates method calls to a parent or sibling class of type. You can not use this for member variables.

answered Dec 6, 2018 at 5:44

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.