|
|
|
|
|
by kazinator
2244 days ago
|
|
Python contains semantic cluster-fumbles. For instance: def fun(listarg = [])
listlocal = []
listarg.append(3)
listlocal.append(3)
It turns out listarg is bound to a list which is not freshly instantiated each time the function is called (with no corresponding argument), unlike listlocal. The expression is evaluated at the time the function is defined, not at call time. The value is stashed somewhere and that value is used for initializing listarg by default.I learned about this from ... running pylint3 on some code which found a buggy use of such a list. This is probably that way for performance because Python doesn't have true literals. [1] is more like (list 1) in Lisp; it's a constructor that has to be executed, producing a newly allocated object; it is not like '(1) which is just a literal object that can be embedded into the compiled program image. Python literature incorrectly refers to [] as a literal, which is bad education: a disservice to newbies who deserve to understand what is a literal. The fact that you can do "x = []" and then safely append to to it proves that it's not a literal, because literal is an abbreviation of "literal constant", which is also something newbies should be taught. Students of CS must absolutely learn the crucial difference between variable initialization and assignment. Python conflates the two. x = 42
def fun():
x = 43 # defines and binds local x.
This was not even fixed in Python for a long time; now you can assign to the global one with a global statement. The concept is bad here and damaging to newbie brains. |
|