stefan <[email protected]> added the comment:
Thank you for your reply and the lucid explanation.
On Monday, May 20, 2019, 9:17:34 PM EDT, Josh Rosenberg
<[email protected]> wrote:
Josh Rosenberg <[email protected]> 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 <[email protected]>
<https://bugs.python.org/issue36980>
_______________________________________
----------
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue36980>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com