Ghost Ark
Ghost Ark

Reputation: 61

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. 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. 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. I apologize if it's a really dumb question for an easy fix.

Upvotes: 1

Views: 92

Answers (2)

StormRider
StormRider

Reputation: 432

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(). That is why you always keep the initial values.

Upvotes: 0

PoweredBy90sAi
PoweredBy90sAi

Reputation: 1233

You need to re point your reference at the Dishes tuple returned from your function. Whats confusing you is that you are using the variable 'i' everywhere. 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. 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.

Upvotes: 2

Related Questions