1

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.

user3483203
51.3k10 gold badges72 silver badges104 bronze badges
asked Jul 16, 2018 at 21:20
3
  • 2
    You're creating a new Dish, with a new price, but printing out the price of the old one. Commented Jul 16, 2018 at 21:31
  • Because you are discarding your return value. You never assign the result of the function call to anything. Commented Jul 16, 2018 at 21:59
  • I see. Following up with csevier's comments and answer, I was able to apply what you said. Thank you for your help and time. Commented Jul 16, 2018 at 22:32

2 Answers 2

2

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.

answered Jul 16, 2018 at 21:45
Sign up to request clarification or add additional context in comments.

2 Comments

Oh, I see. Thank you so much for your help. Also, when I print(i.price) I received "AttributeError: 'float' object has no attribute 'price'", but I was able to realize that i was set to become that value that I wanted to print. So I just changed it to print(i) which worked out. Again, thanks so much for the explanation.
glad I could help.
0

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.

answered Jul 16, 2018 at 22:29

1 Comment

Thanks for your answer! This one makes a lot of sense to me as well.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.