【发布时间】:2019-01-05 19:47:48
【问题描述】:
如果我有一个调用的类有一个调用该类中编写的其他函数的函数,我怎样才能使它与多个对象一起工作。例如
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。我意识到我编写它的方式不适用于多个对象。
【问题讨论】:
-
“同时使用两个对象”是什么意思?你想写一行导致对
obj_one.do_both和obj_two.do_both的调用吗? -
您在某些地方使用
self,但在其他地方忘记使用 -
我认为您需要描述围绕这个问题的上下文。您在这里尝试实现的目标可能不是一个好的最终目标。
-
您始终可以将对象作为参数传递给函数。例如
def my_func(self, other_obj),那么你可以拨打self.do_something()和other_obj.other_method()。 -
我的意图是让 obj_one 和 obj_two 调用。两者都做 ()。