Why is this function undefined in this case

Why does the usage of the withdraw function (in the if statement in the transfer function) make an error saying the function is undefined?

class Category:

    def __init__(self, name):
        self.ledger = []
        self.cat_name = name
        self.balance = 0
    def deposit(self, amount, description=""):
        self.balance += amount
        self.ledger.append({"amount": amount, "description": description})

    def withdraw(self, amount, description=""):
        if self.balance - amount >= 0:
            self.balance -= amount
            self.ledger.append({"amount": -1 * amount, "description": description})
            return True
        return False
    def get_balance(self):
        return self.balance
    def transfer(self, newcat, ammount):
        if(withdraw(amount, "Transfer to " + newcat.cat_name)):

All of these functions are defined as methods on instances of the object itself, much like the internal variables.

sorry I am not sure I understand what you mean

The balance variable is defined on the object itself. So in the withdraw method.

so how can I correct the error I am getting

Do you know how to call a method on an object in Python? For example, how you call .sort on a list in Python?

1 Like

ohh I understand now. I use self as the instance of the class and call the withdraw method on that.
like self.withdraw(amount, "Transfer")

1 Like

Bingo. Inside of the definition of the class, that’s how you call methods for the object.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.