+ 1

Why i get output like this? In the last line of code i just print(f) but the output is different from the input variable.

def myfunc(v) v*=3 print(v) v=3 myfunc(v) print(v) f=[1,2] myfunc(f) print(f) output is: 27 3 [1,2,1,2,1,2] [1,2,1,2,1,2] 👈how i get this kind of output

4th Nov 2018, 6:53 AM
fenil
fenil - avatar
4 Answers
+ 1
Arrays are object, so you pass in argument the address of the object. Then, the function modify the object, that's why you get a different output.
4th Nov 2018, 8:56 AM
Théophile
Théophile - avatar
+ 1
Excuse me if you don't understand, I'm a bad teacher! 😱
4th Nov 2018, 8:56 AM
Théophile
Théophile - avatar
+ 1
thank you so much Théophile
4th Nov 2018, 8:59 AM
fenil
fenil - avatar
+ 1
You're welcome!
4th Nov 2018, 8:59 AM
Théophile
Théophile - avatar