|
| 1 | +''' |
| 2 | +When we pass an object to a parameter, the parameter name becomes a reference variable. |
| 3 | + |
| 4 | +Recollecting the balloon example, it is like creating one more ribbon to the same balloon. |
| 5 | +Thus there is one object with two reference variable, one the formal parameter and the actual parameter. |
| 6 | +Thus any change made through one reference variable will affect the other as well. |
| 7 | +''' |
| 8 | + |
| 9 | +class Mobile: |
| 10 | + def __init__(self, price, brand): |
| 11 | + self.price = price |
| 12 | + self.brand = brand |
| 13 | + |
| 14 | + def change_price(mobile_obj): |
| 15 | + print ("Id of object inside function", id(mobile_obj)) |
| 16 | + mobile_obj.price=3000 |
| 17 | + |
| 18 | +mob1=Mobile(1000, "Apple") |
| 19 | +print ("Id of object in driver code", id(mob1)) |
| 20 | + |
| 21 | +mob1.change_price() |
| 22 | +print ("Price of mob1 ", mob1.price) |
0 commit comments