Assignment versus binding [was Re: unintuitive for-loop behavior]

Steve D'Aprano steve+python at pearwood.info
Mon Oct 3 22:41:29 EDT 2016


On Mon, 3 Oct 2016 04:15 pm, Jussi Piitulainen wrote:

> Steve D'Aprano writes:
>> Why shouldn't people say that binding and assignment are the same
>> thing in Python? What's the difference?
> 
> Outside Python, (lambda x : f(x)) is said to "bind" x. It's different
> from assigning a new value to x. It's similar to how quantifiers in
> logic are said to bind their variables, so that variables bound to
> different quantifiers are different variables.

o_O

Me: "How is binding different from assignment?"

You: "This is an example of binding: lambda x: f(x). Its different from
assigning to x. Clear now?"


What's "outside python"? Can you give some examples? Presumably you don't
actually mean "every single programming language apart from Python", even
if that's what it sounds like.

Anyway, I've done my own research, and what I've found is:

- Some people consider assignment and binding to be synonyms. This is
especially common in OOP languages (like Python), and procedural languages.

- Some people consider them to be different. This is especially common in
functional languages with an academic emphasis.

For those who consider them different, the distinction is usually along
these lines:

- Assignment associates a mutable label to a value. So if I say "x = 1",
that sets up a labelled box and puts 1 inside that box. Then if I say "x =
2", the box is updated to contain 2 instead of 1.

- Binding is more like setting up a label or identifier for a constant. For
example, in a legal contract, you might say words to the effect of:

   John and Jane Smith, of 123 Evergreen Terrace, Metropolis ("the Client")

and then everywhere the contract refers to "the Client", you are supposed to
mentally substitute "John and Jane Smith, of ...". Its a fixed label for a
fixed value. Even if the value is mutable (John and Jane have a baby, and
the baby is now automatically covered by the contract, the value cannot be
replaced with a new value (you can't swap out John and Jane and replace
them with their neighbours Fred and Ethel, you have to cancel the contract
and write a new one).

So for these languages, bindings relate to a *fixed* association between a
label and value. Whether the *value* itself is mutable or immutable, the
association is fixed.



-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list