osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Can global variable be passed into Python function?


Jussi Piitulainen <jpiitula at ling.helsinki.fi>:

> 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.

But of course there are variables in Python:

   By ?frozen? we mean that all local state is retained, including the
   current bindings of local variables, [...] (<URL:
   http://docs.python.org/3.2/reference/simple_stmts.html
   #the-yield-statement>)

   The public names defined by a module are determined by checking the
   module?s namespace for a variable named __all__ (<URL:
   http://docs.python.org/3.2/reference/simple_stmts.html
   #the-import-statement>)

   It would be impossible to assign to a global variable without global
   (<URL: http://docs.python.org/3.2/reference/simple_stmts.html
   #the-global-statement>)

etc etc.

However, your point about "observable behavior" is key, and Python users
of all people should get the principle (as it is related to duck
typing).

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

Maybe the idea comes from the fact that you can't easily pass a
variable to a function for modification.

Consider this C function:

   void make_printable(const char **ref)
   {
       if (!*ref)
           *ref = "<NULL>";
   }

which allows:

   make_printable(&x);
   make_printable(&s->name);
   make_printable(&a[i]);


Marko