0

I am new to OOP and practicing by writing a budget class as asuch:

class Budget:
    category_record = []
    def __init__(self, category = '', balance = 0):
        self.category = category
        self.balance = balance
        
        Budget.category_record.append(self)
        
    
    def deposit(self, amount):
        self.balance += amount
        print(f"You have deposited {amount} into your {self.category} Budget, your balance is {self.balance}")
        return

    def withdraw(self, amount):
        if amount > self.balance:
            print('Insufficient Balance, unable to withdraw')
        else:
            self.balance -= amount
        print(f"You have withdrawn {amount} from your {self.category} Budget, your balance is {self.balance}")
        return 
    
    def category_balance(self):
        print(f'Your balance is {self.balance} for your {self.category} budget')

IM trying to keep a record of all instances of the budget class as a method(Forgive me if my terms are wrong, still getting used to them)


# Instantiate Food budget 
food = Budget('food')

# deposit 200 into food budget
food.deposit(200)

# withdraw 100 from food budget
food.withdraw(100)

#instantaite rent budget
rent = Budget('house', 5000)

# check balance of rent budget
rent.category_balance()

Such that when I call a record method on the budget class I can get a list of ['food', 'rent'] or if possible a dictionary with the key as category and value as balance {'food':100...}

2
  • It looks like you are on the right track. You want to create some @classmethod methods that will access the Budget objects stored in your category_record list. Commented Apr 11, 2021 at 16:15
  • Hey, I am curious why you want to keep track of instances of Budget ? Commented Apr 11, 2021 at 16:25

2 Answers 2

1

To elaborate on my @classmethod comment:

class Budget:
    category_record = []
    def __init__(self, category = '', balance = 0):
        self.category = category
        self.balance = balance

        Budget.category_record.append(self)


    def deposit(self, amount):
        self.balance += amount
        print(f"You have deposited {amount} into your {self.category} Budget, your balance is {self.balance}")
        return

    def withdraw(self, amount):
        if amount > self.balance:
            print('Insufficient Balance, unable to withdraw')
        else:
            self.balance -= amount
        print(f"You have withdrawn {amount} from your {self.category} Budget, your balance is {self.balance}")
        return

    def category_balance(self):
        print(f'Your balance is {self.balance} for your {self.category} budget')

    @classmethod
    def budget_list(cls):
        if len(Budget.category_record)== 0:
            print("No budgets created")
        else:
            print("Budget Categories:")
            for budge in Budget.category_record:
                print(budge.category)

    @classmethod
    def full_report(cls):
        if len(Budget.category_record)== 0:
            print("No budgets created")
        else:
            print("Budget Balances:")
            for budge in Budget.category_record:
                print(f"{budge.category} : {budge.balance}")




# Instantiate Food budget
food = Budget('food')

# deposit 200 into food budget
food.deposit(200)

# withdraw 100 from food budget
food.withdraw(100)

#instantaite rent budget
rent = Budget('house', 5000)

# check balance of rent budget
rent.category_balance()

Budget.budget_list()

Budget.full_report()
Sign up to request clarification or add additional context in comments.

Comments

1

If you want to get a list of all of the instances of the Budget class, your method can just return the category_record list in which you have already been storing all instances as they are created. However, it seems you want the list to contain the names (identifiers) of the variables to which you are assigning the instances. The Budget class will have no way to access those names, so instead, I recommend you try and use the category names as that will be recorded in your category_record variable.

From your question I'm assuming you want the method to be called record. The best way to do this is to use a class method with the built-in classmethod decorator. These methods act on the class itself, and not an instance, and thus take a cls parameter instead of a self parameter (both of those names are only the convention, however I strongly advise against straying from the convention as it will make your code unnecessarily complex for everyone else). For example:

class Budget:
    ...
    # other code
    ....
    category_record = []

    @classmethod
    def record(cls):
        return [budget.category for budget in cls.category_record]
        # If you are unfamiliar with this syntax, search up "list comprehensions"

# Access the list
print(Budget.category_record)

If you want a dictionary instead, you can replace that method with this:

class Budget:
    ...
    # other code
    ...
    category_record = []

    @classmethod
    def record(cls):
        return {budget.category:budget.balance for budget in cls.category_record}
        # If you are unfamiliar with this syntax, search up "dictionary comprehensions"

# Access the dictionary
print(Budget.category_record)

Comments

Your Answer

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.