Le 08/05/2014 02:35, Ben Finney a écrit :
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.

For me, names bound to values is the same concept as pointer pointing to memory. bar = foo copies the pointer and not the underlying memory. This is not a foreign concept to C programmers.


---
Ce courrier électronique ne contient aucun virus ou logiciel malveillant parce 
que la protection avast! Antivirus est active.
http://www.avast.com


--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to