Can global variable be passed into Python function?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Feb 21 22:28:16 EST 2014


On Fri, 21 Feb 2014 07:13:25 -0500, Ned Batchelder wrote:

> 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.  It has variables
> (names associated with values, and the values can change over the course
> of the program), they just don't work the same as C or Fortran
> variables. In fact, they work exactly the same as Javascript or Ruby
> variables.

I sympathise with your view. It seems quite ridiculous to claim that 
Python has no variables. If it has no variables, what on earth does it 
mean when we say "x = 42"?

But the very ridiculousness is what gives it the attention-grabbing power 
that makes it a useful meme. "Python variables don't behave like C 
variables" might be true, but it's also wish-washy and forgettable.

In my own case, I never quite got Python's name binding semantics until I 
was introduced to the "Python has no variables" meme. That got my 
attention long enough to listen to the actual message: my assumptions 
about how variables behave was based on Pascal semantics, and Python 
doesn't quite follow the same rules. Consequently, if I implicitly define 
"variable" to mean "Pascal variables", as I had been, then Python has no 
variables, it has these things called "name bindings".

That's when I got it.

I went through a phase where I too insisted that Python had no variables. 
But then my natural laziness asserted itself, and I decided that the word 
"variable" is too useful to always reject it (and besides, C- and Pascal-
like languages don't have a monopoly on the word "variable"). Now, I use 
the terms "variable" or "reference" or "name binding" as I feel makes the 
most sense in context, depending on my best guess of the risk of 
misunderstanding or confusion.


-- 
Steven



More information about the Python-list mailing list