1

I am sure there must be a duplicate of my question, but have not found it. I am a novice trying to finally learn OOP. In the following code I have three levels of classes - the sub-class seems to be inheriting attributes from the base class, but not from its immediate parent:

class Option(object):
 def __init__(self, *args, **kwargs):
 self.strike_type = kwargs.get('strike_type')
 self.multiplier = kwargs.get('mutiplier', 100)
 self.exp_months = kwargs.get('exp_months', 1)
 self.strike_steps = kwargs.get('strike_steps', 1)
class Put(Option):
 def __init__(self, *args, **kwargs):
 super(Option, self).__init__(*args, **kwargs)
 self.option_type = 'put'
class ShortPut(Put):
 def __init__(self, *args, **kwargs):
 super(Put, self).__init__(*args, **kwargs)
 self.ratio = kwargs.pop('ratio', 1)
 self.qty_mult = -1
shortput = ShortPut(strike_type=-1, exp_months=6, strike_steps=2, ratio=2)
shortput.ratio #class ShortPut
2
shortput.exp_months #class Option
6
shortput.option_type #class Put
AttributeError: 'ShortPut' object has no attribute 'option_type'
dir(shortput) #dunder entries removed
['exp_months',
'multiplier',
'qty_mult',
'ratio',
'strike_steps',
'strike_type']

So the attribute works fine if I cut and paste it into either Option or ShortPut. I have also tried change the order in the init modules, but it doesn't seem to make a difference if the call to super is made before or after the other attributes. The arguments are flowing through from ShortPut to Put to Option, but it doesn't seem to like the attribute in the middle class.

followup - I can't call the put class directly:

put = Put(strike_type=-1, exp_months=6, strike_steps=2, ratio=2)
TypeError: object.__init__() takes no parameters

Any insights into what is going on would be greatly appreciated.

asked Aug 29, 2013 at 16:57

1 Answer 1

2

When you use super, the first argument should be the class from which you are making the call, not its superclass. So in Put you should use super(Put, self) and in ShortPut you should use super(ShortPut, self).

answered Aug 29, 2013 at 16:59
Sign up to request clarification or add additional context in comments.

2 Comments

+1 -- One of the points of using super is exactly this: you don't have to hard-code the superclass, thus making easier using multiple-inheritance code where the mro may not be obvious.
I get it now, thanks. What tripped me up was that I thought the self argument was referring to it's class, but I see now it is binding to a given instance of class (like I said I am still learning OOP).

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.