Marko Rauhamaa <ma...@pacujo.net> writes: > Ben Finney <b...@benfinney.id.au>: > > > That's why I always try to say “Python doesn't have variables the way > > you might know from many other languages”, > > Please elaborate. To me, Python variables are like variables in all > programming languages I know.
Many established and still-popular languages have the following behaviour:: # pseudocode foo = [1, 2, 3] bar = foo # bar gets the value [1, 2, 3] assert foo == bar # succeeds foo[1] = "spam" # foo is now == [1, "spam", 3] assert foo == bar # FAILS, ‘bar’ == [1, 2, 3] This is because such languages treat each variable as “containing” a value. Assignment puts another copy of the value in the variable, after which those two values have a distinct history. What happens to one value does not affect the other. Python, on the other hand, has this behaviour:: foo = [1, 2, 3] bar = foo # ‘bar’ binds to the value ‘[1, 2, 3]’ assert foo == bar # succeeds foo[1] = "spam" # ‘foo’ *and* ‘bar’ now == [1, "spam", 3] assert foo == bar # succeeds, ‘bar’ is bound to ‘[1, "spam", 3]’ The assignment statement in Python does not put a value in a container, the way it does for variables in many popular languages. Instead, assignment binds the left-hand-side reference (in these examples, names) to the right-hand-side value. Both remain references to the same value until the binding changes to some other value. So Python doesn't have variables in the way programmers coming from many other languages expect. Instead, it has references bound to values. -- \ “Actually I made up the term “object-oriented”, and I can tell | `\ you I did not have C++ in mind.” —Alan Kay, creator of | _o__) Smalltalk, at OOPSLA 1997 | Ben Finney -- https://mail.python.org/mailman/listinfo/python-list