Python and variables (was: Can global variable be passed into Python function?)
Ned Batchelder <ned at nedbatchelder.com> writes:
> On 2/21/14 2:23 AM, dieter wrote:
> > Sam<lightaiyee at gmail.com> writes:
> >> >I need to pass a global variable into a python function.
> > Python does not really have the concept "variable".
> > What appears to be a variable is in fact only the binding of an
> > object to a name. If you assign something to a variable,
> > all you do is binding a different object to the name.
> Man, do I hate this idea that Python has no variables.
I agree with the sentiment of you've quoted from ?dieter?, above. But I
also agree with your sentiment.
I think it's misleading to use the term ?variable? to describe what
Python has, because that term has an unavoidable collection of
implications in the wider programming community, and many of those
implications are plain false for Python.
I think it's misleading to claim Python ?has no variables? ? which is
not quite the same as what ?dieter? claimed. Python has a model of
binding references to values, and the Python documentation calls these
bindings ?variables? in the special case where the reference is a name.
So, in that sense, Python has variables.
> Python's variables are names bound to values.
The ideal, in my view, would be for the Python documentation to never
use the term ?variable? for this concept, since it leads newcomers
predictably to false inferences that mar their understanding of Python.
The damage isn't small, since it makes a rather simple and clear concept
needlessly difficult to understand, and falsehoods learned about
Python's data model need to be carefully rooted out and repaired later
down the track.
We should, instead, consistently use ?reference? and/or ?binding? for
this concept, since those lead to much more accurate inferences about
what is happening.
\ ?People always ask me, ?Where were you when Kennedy was shot?? |
`\ Well, I don't have an alibi.? ?Emo Philips |