*= operator

BartC bc at freeuk.com
Sat Nov 21 08:37:25 EST 2015


On 21/11/2015 13:20, Cai Gengyang wrote:
> This is a piece of code that calculates tax and tip :
>
> def tax(bill):
>      """Adds 8% tax to a restaurant bill."""
>      bill *= 1.08
>      print "With tax: %f" % bill
>      return bill
>
> def tip(bill):
>      """Adds 15% tip to a restaurant bill."""
>      bill *= 1.15
>      print "With tip: %f" % bill
>      return bill
>
> meal_cost = 100
> meal_with_tax = tax(meal_cost)
> meal_with_tip = tip(meal_with_tax)
>
> Does bill *= 1.08 mean bill = bill * 1.15 ?

bill *= 1.08 would mean bill = bill*1.08 (not 1.15; I assume that's a typo).

Although you can never be 100% sure in Python (as * or *= could have 
been redefined to do something else), there's no reason to suspect that 
here.

Also, there's probably some technical difference that someone could up 
with (*= doing in-place modification for example), but that shouldn't 
matter when bill is just a number.

-- 
Bartc




More information about the Python-list mailing list