+ 3

python pass by value and pass by reference

hi there is question that I have find the answer but i don't know its correct or not the question is ((Define and recall functions for each type of data and data structures, and specify which type of parameters are of the type pass by reference and which one is type pass by value List of items to be tested: int , float , dictionary , tuple set , list , string I write function for all else tuple and figured out that int , float , string are pass by value and dictionary , set , list are pass by reference but I don't know is it true or false please if it is false tell me and in addition i don't know how to write a function to show that tuple is pass by reference or pass by value please help me to write

25th Apr 2020, 10:01 AM
nima rasi
2 Respostas
+ 5
Did you find the question in some tutorial? Because whoever wrote it, doesn't seem to have a good understanding of Python. In Python, arguments are *always* passed by reference. You can check this by calling the id function on a value outside and inside of the function - it's the same. Try this piece of code: def f(x): print('Inside:', id(x)) a = 42 b = 'Hello' c = ['spam', 'eggs'] for val in a, b, c: print('Outside:', id(val)) f(val) Python's way to give some sort of security is to have 'immutable types', but that's really not quite the same thing.
25th Apr 2020, 10:25 AM
HonFu
HonFu - avatar
+ 3
thank you mr HonFu
25th Apr 2020, 10:33 AM
nima rasi