Python variables?

Ned Batchelder ned at nedbatchelder.com
Mon Sep 30 21:28:36 EDT 2013


On 9/30/13 9:03 PM, Steven D'Aprano wrote:
> On Mon, 30 Sep 2013 19:47:49 -0400, Ned Batchelder wrote:
>
>> On 9/30/13 6:37 PM, Ben Finney wrote:
>>> Ethan Furman <ethan at stoneleaf.us> writes:
>>>
>>>>   From [Ned Batchelder]'s blog:
>>>>> Names are Python's variables: they refer to values, and
>>>>>    those values can change (vary) over the course of your program.
>>>> This is partially incorrect.  If the value referred to by the name is
>>>> immutable, then it cannot change; perhaps you meant to say that which
>>>> object the name points to can vary over time?
>>> I agree. Names are not Python's variables.
>>>
>>> If anything in Python is a “variable” as generally understood, it is
>>> not a name. It is a *binding*, from a reference (a name, or some other
>>> reference like a list item) to a value.
>>>
>>> It is the binding which can change over the course of the program, so
>>> that is the variable.
>>>
>>>
>> True, but no one calls the binding the variable.  Here is a program:
>>
>>       x = 4
>>
>> Every one of us is perfectly comfortable talking about the variable x.
>> Don't get hung up on implementation pedantry.  The name x here refers to
>> 4.  Later it could refer to "four".  The value associated with the name
>> x changed.  x is a variable.
> Your statement is ambiguous -- do you mean the *association* between
> value (object) and name changed, or the value which is associated with
> the name changed?

Yes, my statement was ambiguous.  The value of x at time t1 can be 
different than the value of x at time t2, and there are two different 
ways it can differ.  None of that changes the fact that the value 
associated with the name varies over the course of a program, giving 
rise to "variables."

I prefer to say that Python has variables, and they work by a mechanism 
of names referring to values.  I don't find that beginners get it by 
being told that Python has no variables.  It seems to be something that 
experts sometimes find helpful, though.

--Ned.
>
> In the first case, "the value associated with the name x changed" is no
> different from saying "the name binding changed", only longer and more
> ambiguous.
>
> In the second case, if you mean that the association remains the same,
> but the value itself changed, that's demonstrably untrue since 4 is
> immutable. But you know that :-)
>
> I straddle the fence on this dispute... I'll often refer to Python
> variables when, in my option, it doesn't confuse the issue or introduce
> ambiguity, but I feel guilty doing so :-) And I always look for the
> opportunity to introduce the concept of name binding into the discussion.
> I'm just not religious about it.
>
>




More information about the Python-list mailing list