africanxmamba
africanxmamba

Reputation: 113

__str__(self) function not working for printing objects in Python

I have been working on improving using classes and objects in Python but have been having trouble displaying the values of different objects in my programs. I was taught by my instructor that using the str(self) function will automatically be called whenever an object is passed the print() function, however, the output still shows the object's address in memory rather than the value. I have documented my code below ran it in two different compiling programs but still cannot find where my error is occurring.

# the class of the bank account
class BankAccount:
    def __init__(self, bal):
        self.__balance = bal

# deposits the amount given by the user
def deposit(self, amount):
    self.__balance += amount

# withdraws the amount given by the user
def withdraw(self, amount):
    if(self.__balance >= amount):
        self.__balance -= amount
    else:
        print("Error: Insufficient funds")

# returns the current balance of the user's account
def get_balance(self):
    return self.__balance

# sets the user's account given a specified balance
def set_balance(self, bal):
    self.__balance = bal

# prints the user's balance
def __str__(self):
    return "The balance is $" + format(self.__balance, ",.2f")

def main():
    start_bal = float(input("Enter your starting balance: ")) # retrieves balance from the user
    savings = BankAccount(start_bal) # creates an object that hold's the user's balance
    print(savings)

main()

instead of getting the value of the account the program outputs <__main__.BankAccount object at 0x0000025EE6535A90>

Please let me know of anything I can change to correct the issue. Thank you.

Upvotes: 0

Views: 4966

Answers (3)

datadaveshin
datadaveshin

Reputation: 166

It is an indention problem, your class is defined as this only:

# the class of the bank account
class BankAccount:
    def __init__(self, bal):
        self.__balance = bal

Try indenting all the code below this section one indendentation block until you get to the def main() function, such that BankAccount gets the methods you intended it to have:

# the class of the bank account
class BankAccount:
    def __init__(self, bal):
        self.__balance = bal

    # deposits the amount given by the user
    def deposit(self, amount):
        self.__balance += amount

    # withdraws the amount given by the user
    def withdraw(self, amount):
        if(self.__balance >= amount):
            self.__balance -= amount
        else:
            print("Error: Insufficient funds")

    # returns the current balance of the user's account
    def get_balance(self):
        return self.__balance

    # sets the user's account given a specified balance
    def set_balance(self, bal):
        self.__balance = bal

    # prints the user's balance
    def __str__(self):
        return "The balance is $" + format(self.__balance, ",.2f")

def main():
    start_bal = float(input("Enter your starting balance: ")) # retrieves balance from the user
    savings = BankAccount(start_bal) # creates an object that hold's the user's balance
    print(savings)

main()

Upvotes: 1

Cyzanfar
Cyzanfar

Reputation: 7136

You need to overwrite the __str__ operator of your BankAccount class:

class BankAccount:
    def __init__(self, bal):
        self.__balance = bal

    def __str__(self):
        return "The balance is $" + format(self.__balance, ",.2f")


In [25]: start_bal = 2.3
In [27]: savings = BankAccount(start_bal) 

In [28]: print(savings)
The balance is $2.30

Just nest your method correctly within your class

Upvotes: 0

Marios Keri
Marios Keri

Reputation: 100

When main is call and in the print(savings) is executed it prints the str representation of BankAccount class which is the savings memory address and then since your str implementation is defined out side of the class it has nothing to do with the class.

try this:

# the class of the bank account
class BankAccount:
    def __init__(self, bal):
        self.__balance = bal

    # deposits the amount given by the user
    def deposit(self, amount):
        self.__balance += amount

    # withdraws the amount given by the user
    def withdraw(self, amount):
        if(self.__balance >= amount):
            self.__balance -= amount
        else:
            print("Error: Insufficient funds")

    # returns the current balance of the user's account
    def get_balance(self):
        return self.__balance

    # sets the user's account given a specified balance
    def set_balance(self, bal):
        self.__balance = bal

    # prints the user's balance
    def __str__(self):
        return "The balance is $" + format(self.__balance, ",.2f")

def main():
    start_bal = float(input("Enter your starting balance: ")) # retrieves balance from the user
    savings = BankAccount(start_bal) # creates an object that hold's the user's balance
    print(savings)

main()

Upvotes: 0

Related Questions