Python class <__main__.Credit at 0x> question

Viewed 39

I'm practicing python class inheritance. But I find something weird. When I inherit the parent class and try to check the attribute. I cannot access the first attribute and it always returns the memory location. I cannot figure out what happened behind it. Hope to seek some help. Thank you.

Here's my code:

class Account():
def __init__(self, acct_num, open_deposit=100, overdraft_limit=1000, interest_rate=1):
    self.number = acct_num
    self.balance = open_deposit
    self.limit = overdraft_limit
    self.interest = interest_rate

def __str__(self):
    return f'The balance is {self.balance:.2f}'

def check_balance(self):
    print(self.balance)

def deposit(self, dep_num):
    self.balance += dep_num

def withdraw(self, wd_num):
    if self.balance + self.limit >= wd_num:
        self.balance -= wd_num
    else:
        return 'Your balance is insufficient'

def add_interest(self):
    self.balance *= (1 + (self.interest / 100))

class Credit(Account):
def __init__(self, withdrawl_rate=5):
    self.wth_rate = withdrawl_rate
    super().__init__(self)
    # super().__init__(self, open_deposit, overdraft_limit, interest_rate)

def __str__(self):
    return f'Credit Account: # {self.number} \nBalance: {Account.__str__(self)}'

def withdraw(self, wd_num):
    total_amount = wd_num * (1 + (self.wth_rate / 100))
    Account.withdraw(self, total_amount)

def change_limit(self, limit):
    self.limit = limit


x = Credit()
x.number
Out[]: <__main__.Credit at 0x109c13250>

Other attributes work well:

x.wth_rate
Out[]: 5
x.balance
Out[]: 100
x.add_interest()
x.balance
Out[]: 101.0
1 Answers

You're passing self to the __init__ function if the base class, which already gets by default self, like other instance functions.

So the self you're passing is the first argument for the base class init, which is acct_num, so it will just cotain a pointer to the instance.

You need to call super().__init__(acct_num=SOME_VALUE) for example to get the desired behavior.

Related