1

I'm having a hard time understanding how to initialize an inherited class in python OOP.

I cannot figure out what arguments need to be passed when I initialize it. These are the classes I'm using:

class BankAccount: #parent class
 def __init__(self, owner, balance):
 self.owner = owner
 self.balance = balance
 def withdrawal(self, withdraw):
 if withdraw > self.balance:
 raise RuntimeError('Sorry, Insufficient Funds!')
 else:
 print('Withdrawal accepted.')
 self.balance -= withdraw
 show_balance = input('See account balance? enter y or n: ')
 if show_balance == 'y':
 print(self.balance)
 def deposit(self, amt):
 self.balance += amt
 print('Deposit Accepted')
 show_balance = input('See account balance? enter y or n: ')
 if show_balance == 'y':
 print(self.balance)
class MinimumBalanceAccount(BankAccount): #child class
 minimum_balance = 100
 def __init__(self):
 BankAccount.__init__(self)
 def withdrawal(self, withdraw):
 if self.balance - withdraw < self.minimum_balance:
 print('Error, balance cannot go below minimum value: {}'.format(minimum_balance))
 else:
 self.balance -= withdraw

But when I try to initialize the child class:

acc2 = MinimumBalanceAccount('Milind', 1000) # I am not sure what to pass as arguments here

Python gives me this error:

TypeError Traceback (most recent call last)
<ipython-input-9-85e55fb15340> in <module>
----> 1 acc2 = MinimumBalanceAccount('milind', 1000)
TypeError: __init__() takes 1 positional argument but 3 were given

What do I pass in as the arguments?? What's going wrong?

Reblochon Masque
37k10 gold badges60 silver badges85 bronze badges
asked Jun 16, 2020 at 8:44
2
  • u need to receive the arguments in the child class constructor as well and pass it to super class. without having the params in init for MinimumBalanceAccount, you cannot pass it to BankAccount's init Commented Jun 16, 2020 at 8:46
  • MinimumBalanceAccount.__init__ takes zero arguments, which is what you're seeing. You have three choices: Let MinimumBalanceAccount accept the same arguments as BankAccount and pass them through to it; let MinimumBalanceAccount accept no arguments but supply its own default arguments when calling BankAccount.__init__, let MinimumBalanceAccount not call BankAccount.__init__ at all. Of those you probably want the first option. And since MinimumBalanceAccount.__init__ doesn't appear to be doing anything, you can omit it entirely. Commented Jun 16, 2020 at 8:48

3 Answers 3

2

You need to pass the required arguments to the subclass, and to the superclass:

class BankAccount:
 def __init__(self, owner, balance):
 self.owner = owner
 self.balance = balance
 def withdrawal(self, withdraw):
 if withdraw > self.balance:
 raise RuntimeError('Sorry, Insufficient Funds!')
 else:
 print('Withdrawal accepted.')
 self.balance -= withdraw
 show_balance = input('See account balance? enter y or n: ')
 if show_balance == 'y':
 print(self.balance)
 def deposit(self, amt):
 self.balance += amt
 print('Deposit Accepted')
 show_balance = input('See account balance? enter y or n: ')
 if show_balance == 'y':
 print(self.balance)
class MinimumBalanceAccount(BankAccount):
 minimum_balance = 100
 def __init__(self, owner, balance):
 super().__init__(owner, balance)
 self.minimum_balance = MinimumBalanceAccount.minimum_balance
 def withdrawal(self, withdraw):
 if self.balance - withdraw < self.minimum_balance:
 print('Error, balance cannot go below minimum value: {}'.format(minimum_balance))
 else:
 self.balance -= withdraw
acc2 = MinimumBalanceAccount('Milind', 1000)

In this case, as pointed out by @Deceze in the comments, you can omit __init__ entirely:

class MinimumBalanceAccount(BankAccount): #child class
 minimum_balance = 100
 def withdrawal(self, withdraw):
 if self.balance - withdraw < self.minimum_balance:
 print('Error, balance cannot go below minimum value: {}'.format(minimum_balance))
 else:
 self.balance -= withdraw
answered Jun 16, 2020 at 8:49
Sign up to request clarification or add additional context in comments.

2 Comments

Or you should omit MinimumBalanceAccount.__init__ entirely...
Yes, I noticed that, thanks; I thought it would be a good idea to keep it as an illustration for the use of super()... I added the assignment of minimum_balance as an instance variable so __init__ is needed
1

You need to add the initializing parameters also to your child Class when you define the __init__ function and pass it to the parent.

class MinimumBalanceAccount(BankAccount): #child class
 minimum_balance = 100
 def __init__(self, owner, balance):
 BankAccount.__init__(self, owner, balance)
answered Jun 16, 2020 at 8:48

1 Comment

It should be noted that __init__ here can be omitted entirely, since it doesn't do anything. If you keep it, you should use super().
0

class MinimumBalanceAccount(BankAccount): #child class

minimum_balance = 100
def __init__(self,owner, balance):
 BankAccount.__init__(self,owner,balance)
def withdrawal(self, withdraw):
 if self.balance - withdraw < self.minimum_balance:
 print('Error, balance cannot go below minimum value: {}'.format(minimum_balance))
 else:
 self.balance -= withdraw
answered Jun 16, 2020 at 9:01

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.