Can global variable be passed into Python function?

Jussi Piitulainen jpiitula at ling.helsinki.fi
Fri Feb 21 03:55:34 EST 2014


dieter writes:
> Sam 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.
> 
> Thus, if you have:
> 
>       i = 1
>       def f(x): x = 5
> 
>       f(i)
> 
> Then "i" will remain "1" and not become "5".
> The effect of "x = 5" is that the name "x" gets bound to "5"
> (where is formerly was bound to "1").

In alleged contrast, the observable behaviour of languages that "have
variables" is the same. This is not considered confusing by the people
who insist that there are no variables in Python.

Note: I do not object to Python's choice of different preferred
terminology. Python seems to be doing fine with that. I do object to
the insistence that Python needs this different terminology because it
behaves differently from the languages where variables are called
variables. Python behaves the same.

Here's Java, where I demonstrate with both a class variable i and a
local variable j; I used String so that this same demonstration will
still serve after someone points out that Java's primite numbers are
not quite objects, which is true but irrelevant:

$ cat Var.java
public class Var {
    public static String i = "1";
    public static String f(String x) {
        x = "5";
        return x;
    }
    public static void main(String... args) {
        String j = "2";
        System.out.println("i == " + i + ", j == " + j);
        System.out.println("f(i) == " + f(i) + ", " +
                           "f(j) == " + f(j));
        System.out.println("i == " + i + ", j == " + j);
    }
}
$ javac Var.java 
$ java -cp . Var 
i == 1, j == 2
f(i) == 5, f(j) == 5
i == 1, j == 2
$ 

This is C, where I again demonstrate with both a global and a local
variable, but here I leave them as ints; I think C might be a language
where the word "variable" has been used to mean something like a block
of memory, which is a different usage; still, would someone be willing
to explain the behaviour of this program by "C lacks variables":

$ cat var.c
#include <stdio.h>
int i = 1;
int f(int x) { x = 5; return x; }
main() {
  int j = 2;
  printf("i == %d, j == %d\n", i, j);
  printf("f(i) == %d, f(j) == %d\n", f(i), f(j));
  printf("i == %d, j == %d\n", i, j);
}
$ gcc -o var var.c
$ ./var 
i == 1, j == 2
f(i) == 5, f(j) == 5
i == 1, j == 2
$ 

This is Scheme, which is where I come from; its variables behave the
same as the corresponding machinery in Python with regard to the
problem at hand:

$ cat var.scm 
(define i 1)
(define (f x) (set! x 5) x)
(let ((j 2))
  (display "i == ") (write i) (display ", j == ") (write j) (newline)
  (display "f(i) == ") (write (f i))
  (display ", f(j) == ") (write (f j)) (newline)
  (display "i == ") (write i) (display ", j == ") (write j) (newline))
$ gsi var.scm 
i == 1, j == 2
f(i) == 5, f(j) == 5
i == 1, j == 2
$ 

> > However, the global variable does not seem to be assigned after
> > the function ends. Is it because parameters are not passed by
> > reference?
> 
> Python lacks the notion of "variable". Thus, it does not
> pass variables into functions but objects.
> The objects, however, get passed by reference.

I think the relevant answer is simply that i and x are different
variables. In Python terminology, I think you would have to say that
they are different names. With a slight change, you would need to
explain how i and i are different names (I think), and then you would
introduce the concept of different namespaces.

Python indeed does not pass variables (and this is a relevant), but
neither do the other languages that "have variables".

[snip]



More information about the Python-list mailing list