如果我有一个调用的类,该类具有一个调用该类中编写的其他函数的函数,那么如何使它与多个对象一起使用。例如
class MyClass:
def __init__(self):
self.a = []
def do_somthing(self):
self.a.append(5)
print(self.a)
def do_somthing_else(self):
self.a.append(10)
print(self.a)
def do_both(self):
do_somthing()
do_somthing_else()
obj_one = MyClass()
obj_two = MyClass()
obj_one.do_both()
obj_two.do_both()
我了解如何使该对象与一个对象一起工作,但是我将如何使这两个对象与对象一起工作
import itertools
class Financial_journal:
def __init__(self):
self.date = []
self.expense = []
self.amount_spent = []
self.ballance = [100]
#this func has not been tested
def main_menu(self):
entry =input("Enter the corresponding number:\n1. Add an expense\n2. change an entry\n 3.Review journal\n4. Exit program").lower()
if entry == "1":
journal_one.add_expense()
if entry == "3":
pass
if entry == "4":
pass
def add_name(self):
pass
def display_journal(self):
entry_num = 0
for d, e, r, b in itertools.zip_longest(self.date, self.expense, self.amount_spent, self.ballance):
entry_num += 1
print("\nentry:", entry_num, "date:" , d, "expense:" , e, "amount spent:" , r,"ballance:", b)
#checks that all lists are not missing data
def missing_data(self):
#prompts user to edit missing dates
for index, entry in enumerate(self.date):
if entry == "None":
journal_one.display_journal()
item = input("\nthe date of the expense on entry {}, if you know the date enter it, if you do not, press continue and enter the date when it is discovered.".format(index +1)).capitalize()
if item == "continue":
journal_one.display_journal()
else:
self.date[index] = item
print("Your journal was updated.")
#prompts user to edit missing expenses
for index, entry in enumerate(self.expense):
if entry == "None":
journal_one.display_journal()
item = input("\nthere was no expense on entry {}, if you know the item was purchased, enter it, if you do not, press continue and enter the expense when it is discovered.".format(index +1)).capitalize()
if item == "continue":
journal_one.display_journal()
else:
self.expense[index] = item
print("Your journal was updated.")
#promts user to add missing amounts spent
for index, entry in enumerate(self.amount_spent):
if entry == "None":
journal_one.display_journal()
item = int(input("\nthe amount that was spent on entry {} is missing, if you know the amount that was spent, enter it, if you do not, press continue and enter the amount when it is discovered.".format(index +1)))
if item == "continue":
journal_one.display_journal()
else:
self.amount_spent[index] = int(item)
self.ballance[index] = self.ballance[index] - sum(self.amount_spent)
print("Your journal was updated.\n")
journal_one.display_journal()
def add_expense(self):
journal_one.missing_data()
self.date.append('None')
self.expense.append("None")
self.amount_spent.append(0)
self.ballance.append((self.ballance[-1]))
entry = input("\nenter the date the purchase was made. ").capitalize()
if entry != "Continue":
self.date[-1] = (entry)
entry = input("were was the purchase made? ").capitalize()
if entry != "Continue":
self.expense[-1] = (entry)
entry = input("enter the amount of money that was spent. ").capitalize()
if entry != "Continue":
self.amount_spent[-1] = int((entry))
self.ballance[-1] = self.ballance[-1] - (self.amount_spent[-1])
journal_one.display_journal()
journal_one = Financial_journal()
journal_one.add_expense()
add_expense()调用missing_data()和display_journal(),而missing_data()调用display_journal。我意识到我编写它的方式不适用于多个对象。
答案 0 :(得分:0)
您遇到的问题是,您正在引用在类本身中创建的特定类实例。相反,您应将journal_one
类内部的Financial_journal
的所有实例替换为self
。这将在最初调用add_expense()
的同一对象上调用以下每个方法。
让我知道这是否有意义,或者您是否需要我进一步解释。