Josh Rosenberg <shadowranger+pyt...@gmail.com> added the comment:

1. This is a bug tracker for bugs in the Python language spec and the CPython 
interpreter, not a general problem solving site.

2. The ids will differ for changeValue2 if you actually call it (kernel = 
kernel + 2 requires the the old id of kernel differ from the new id, because 
they both exist simultaneously, and are different objects); I'm guessing you're 
calling the wrong function or printing the ids of the wrong variables.

3. "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" Every bit of clobbering 
you're seeing involves a variable passed to the function (and sometimes 
returned by it). CVkernel=myKernel just made two names that bind to the same 
underlying object, so passing either name as an argument to a function that 
modifies its arguments will modify what is seen from both names. That's how 
mutable objects work. This is briefly addressed in the tutorial here: 
https://docs.python.org/3/tutorial/classes.html#a-word-about-names-and-objects 
. As a general rule, Python built-ins *either* modify their arguments in place 
and return nothing (None) *or* return new values leaving the arguments 
unmodified. It's a matter of programmer discipline to adhere to this practice 
in your own code (numpy does make it harder, since it uses views extensively, 
making sli
 cing not an effective way to copy inputs).

All that said, this isn't a bug. It's a longstanding feature of Python alias 
arguments to avoid expensive deep copies by default; the onus is on the 
function writer to copy if needed, or to document the behavior if mutation of 
the arguments is to occur.

----------
nosy: +josh.r

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue36980>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to