[issue36980] pass-by-reference clues

Steven D'Aprano report at bugs.python.org
Mon May 20 21:15:40 EDT 2019


Steven D'Aprano <steve+python at pearwood.info> added the comment:

Hi Stefan, and welcome. 

This is not a help desk, you really should ask elsewhere for explanations of how Python works. There are no bugs here: what you are seeing is standard pass-by-object behaviour.

You are misinterpreting what you are seeing. Python is never pass by reference or pass by value.

https://en.wikipedia.org/wiki/Evaluation_strategy

https://www.effbot.org/zone/call-by-object.htm

*All* function objects, whether strings, ints, lists or numpy arrays, are passed as objects. If you want to make a copy, you have to explicitly make a copy. If you don't, and you mutate the object in place, you will see the mutation in both places.


> Shouldn't the id of each variable be different if they are different instances?

Not necessarily: IDs can be reused. Without seeing the actual running code, I can't tell if the IDs have been used or if they are the same ID because they are the same object.

> Would it possible for the python interpreter/compiler to let me know when a function is going to clobber a variable that is not used in the function or passed to the function or returned by the function

Python never clobbers a variable not used in the function. It may however mutate an object which is accessible from both inside and outside a function.

----------
nosy: +steven.daprano
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36980>
_______________________________________


More information about the Python-bugs-list mailing list