简体   繁体   English

Python:当我在另一个函数中调用一个函数时,没有变化

[英]Python: No change when I call a function inside another function

so some context, my first function is this one right here (The one I am trying to call): 因此,在某些情况下,我的第一个功能就是这里的那个功能(我正在尝试调用的那个功能):

def Dish_change_price(dish: Dishes, change: int) -> float:
    percent = change/100
    dish = dish._replace(price=dish.price + dish.price*percent)
    return dish.price

With that, I would call it in this function: 这样,我将在此函数中调用它:

def Dishlist_change_price(dishlist: DL, change: int) -> float:
    for i in dishlist:
        Dish_change_price(i, change)
        print(i.price)

After doing the function (Dishlist_change_price(DL, 100)) I would expect it to print the prices doubled (the change is represented as percentages), but it would print the price unchanged. 执行完函数(Dishlist_change_price(DL,100))之后,我希望它打印出的价格翻倍(更改以百分比表示),但是打印出的价格不变。 However, when I just take the body of Dish_change_price function, and place it directly into the Dishlist function, it does what I want it to do. 但是,当我只拿Dish_change_price函数的主体并将其直接放入Dishlist函数中时,它便完成了我想要的工作。 As in: 如:

def Dishlist_change_price(dishlist: DL, change: int) -> float:
     percent = change/100
     for i in dishlist:
         i = i._replace(price=i.price + i.price * percent)
         print(i.price)

Here is the list for reference: 这是供参考的列表:

from collections import namedtuple
Dishes = namedtuple('Dishes', 'name price calories')
d1 = Dishes("Filet Mignon", 40, 720)
d2 = Dishes("Fried Chicken", 10, 1500)
d3 = Dishes("Sake Salmon", 15, 500)
d4 = Dishes("Filet Mignon", 40, 720)
d5 = Dishes("Pho", 11, 600)

D1 = Dishes("Pad Thai", 11, 650)
D2 = Dishes("Dim Sum", 30, 800)
D3 = Dishes("Double-Double Burger", 5, 750)
D4 = Dishes("15 Wings", 10, 900)

DL = [d1, d2, d3, d4, d5]
DL2 = [D1, D2, D3, D4]
DL.extend(DL2)

I don't understand why calling the function directly does not change each of the Dishes but putting in the body will do so. 我不明白为什么直接调用该函数不会更改每个Dishes,而是将其放入体内会更改。 I apologize if it's a really dumb question for an easy fix. 如果这是一个很简单的问题,我很抱歉。

You need to re point your reference at the Dishes tuple returned from your function. 您需要将参考指向从函数返回的Dishes元组。 Whats confusing you is that you are using the variable 'i' everywhere. 令您感到困惑的是,您到处都在使用变量“ i”。 Here, this will work: 在这里,这将起作用:

def Dishlist_change_price(dishlist: DL, change: int) -> float:
    for i in dishlist:
        i = Dish_change_price(i, change)
        print(i.price)

Something to note, your original code would have worked with other object types that are mutable. 需要注意的是,您的原始代码可以与其他可变的对象类型一起使用。 However namedtuples are not, so it becomes a new object reference. 但是namedtuple不是,因此它成为新的对象引用。 If you run print(id(i)) in each one youll see the identity (the place in memory) will be different and you are printing the price of your original. 如果您在每张纸上都运行print(id(i)),您会看到标识(在内存中的位置)会有所不同,并且您正在打印原始价格。

I think you want something like this: 我想你想要这样的东西:

def Dish_change_price(dish: Dishes, change: int) -> Dishes:
    percent = change/100.0
    return dish._replace(price=dish.price + dish.price*percent)

def Dishlist_change_price(dishlist: DL, change: int) -> DL:
    new_list = list()
    for dish in dishlist:
        new_list.append(Dish_change_price(dish, change))
    return new_list

changed_list = Dishlist_change_price(DL, 90)

for dish in changed_list:
    print(dish)

In your example you forgot to return the new dish in Dish_change_price(). 在您的示例中,您忘记了在Dish_change_price()中返回新菜。 That is why you always keep the initial values. 这就是为什么您始终保留初始值的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM