Can global variable be passed into Python function?

Marko Rauhamaa marko at pacujo.net
Fri Feb 28 10:29:54 EST 2014


Chris Angelico <rosuav at gmail.com>:

> Can you elaborate on this "nonliteral constants" point? How is it a
> problem if DISCONNECTING isn't technically a constant? It follows the
> Python convention of being in all upper-case, so the programmer
> understands not to rebind it. Is the problem that someone might
> (naively or maliciously) change the value of DISCONNECTING, or is the
> problem that Python doesn't fundamentally know that it won't change?

This last point. It would make it impossible for Python to treat the
switch statement as anything but an alternate form of chained if-else.
A dict optimization wouldn't actually optimize anything because it would
have to be constructed every time the statement is executed.

   switch self.state from Connection.State:
       case CONNECTING or CONNECTED:
           ...
       case DISONNECTING:
           ...
       else:
           ...

would have to be transformed by Python into:

   _X1 = self.state
   _X2 = Connection.State
   if _X1 is _X2.CONNECTING or _X1 is _X2.CONNECTED:
       ...
   elif _X1 is _X2.DISCONNECTING:
       ...
   else:
       ...

So optimization is gone. Then we have the syntactic burden. Python
currently doesn't (seem to) have a syntactic precedent for such implicit
dot notation. (Note that even Java had to complicate its syntax
analogously with enums.) In "CONNECTING or CONNECTED", "or" wouldn't be
an operator in an expression but a particle.

Another syntactic oddity is the two indentation levels.

BTW, here's a syntax that doesn't introduce any new keywords:

   with self.state from Connection.State:
       if CONNECTING or CONNECTED:
           ...
       elif DISONNECTING:
           ...
       else:
           ...


Marko



More information about the Python-list mailing list