Python OOP 问题,一个 class 的值未被另一个 class 的函数更新

Python OOP question, one class's value is not updated by another class's function

我正在用 OOP 做一个简单的“账单拆分”情况,出于某种原因,该项目的“分区价格”(每当有更多用户参与该项目时更新;例如:1 人想要 5 美元的项目 = 5 美元分区价格,2 个人想要 = $2.5 分区价格,...)。

由于某种原因,Participant 中的“欠款”和 Item 中的 partition_price 不想更新,我不知道出了什么问题。

class Participant:

    #Participant has a name and has items he wants to pitch in for
    def __init__(self, name):
        self.name = name
        self.items = []
        self.owes = []

    #Append the item (Item class) to the item list
    #Append participant's name to the item's participant list
    #Append the item's partition price $ to the owes list
    def add_Item(self, item):
        self.items.append(item)
        item.participants.append(self.name)
        self.owes.append(item.partition_price)

class Item:

    #Item has a name and a price
    def __init__(self, name, price):
        self.name = name
        self.price = price

        #List of participants that are pitching in for the item
        self.participants = []

        #If the list of participants is empty (no participants)
        if not self.participants:
            #The cost of a "pitch in" for this item is [=13=]
            self.partition_price = 0

        #Otherwise, the cost of a "pitch in" for this item is its price/number of participants
        else:
            self.partition_price = price/len(self.participants)

#Create 2 people
matthew = Participant('Matthew')
bob = Participant('Bob')

#Create an item
juice = Item('Apple Juice', 2.99)

#These 2 people wants that item
matthew.add_Item(juice)
bob.add_Item(juice)

#Test
print(f'Matthew wants: {[i.name for i in matthew.items]}')
print(f'Who wants Apple Juice: {juice.participants}')
print(f'Matthew owes: {matthew.owes}')


print(f'The juice costs: {juice.price}, so each person owes: {juice.partition_price} <-- class function')
print(f'The juice ocsts: {juice.price}, so each person owes: {juice.price/len(juice.participants)} <-- external function')

这是它打印的内容:

Matthew wants: ['Apple Juice']
Who wants Apple Juice: ['Matthew', 'Bob']
Matthew owes: [0]
The juice costs: 2.99, so each person owes: 0 <-- class function
The juice ocsts: 2.99, so each person owes: 1.495 <-- external function

Matthew 在第 3 次打印报表时应欠 1.495。第 4 个打印语句也应打印 1.495。只有外部函数有效。

您的 partition_price 正在 Item 的初始化期间计算 当您第一次在行 juice = Item('Apple Juice', 2.99) 中创建 juice 时,participants 将是一个空列表,导致 partition_price 成为 0.

您随后为 matthewbob 调用 add_Item 函数,将它们添加为 juice 的参与者,这解释了为什么 juice.price/len(juice.participants) returns 1.495

您可能希望在 Item 中使用 add_participant 方法,而不是调用 item.participants.append(self.name) 方法,它将参与者添加到 self.participants 并计算 self.partition_price

class Participant:

#Participant has a name and has items he wants to pitch in for
def __init__(self, name):
    self.name = name
    self.items = []
    self.owes = []

#Append the item (Item class) to the item list
#Append participant's name to the item's participant list
#Append the item's partition price $ to the owes list
def add_Item(self, item):
    self.items.append(item)
    item.add_participant(self.name)
    self.owes.append(item.partition_price)

class Item:

    #Item has a name and a price
    def __init__(self, name, price):
        self.name = name
        self.price = price

        #List of participants that are pitching in for the item
        self.participants = []

        #If the list of participants is empty (no participants)
        if not self.participants:
            #The cost of a "pitch in" for this item is [=10=]
            self.partition_price = 0

        #Otherwise, the cost of a "pitch in" for this item is its price/number of participants
        else:
            self.partition_price = price/len(self.participants)
        
        
    def add_participant(self, name):
        self.participants.append(name)
        self.partition_price = self.price / len(self.participants)

#Create 2 people
matthew = Participant('Matthew')
bob = Participant('Bob')

#Create an item
juice = Item('Apple Juice', 2.99)

#These 2 people wants that item
matthew.add_Item(juice)
bob.add_Item(juice)

#Test
print(f'Matthew wants: {[i.name for i in matthew.items]}')
print(f'Who wants Apple Juice: {juice.participants}')
print(f'Matthew owes: {matthew.owes}')


print(f'The juice costs: {juice.price}, so each person owes: {juice.partition_price} <-- class function')
print(f'The juice ocsts: {juice.price}, so each person owes: {juice.price/len(juice.participants)} <-- external function')

输出:

Matthew wants: ['Apple Juice']
Who wants Apple Juice: ['Matthew', 'Bob']
Matthew owes: [2.99]
The juice costs: 2.99, so each person owes: 1.495 <-- class function
The juice ocsts: 2.99, so each person owes: 1.495 <-- external function