Can global variable be passed into Python function?

Marko Rauhamaa marko at pacujo.net
Sat Mar 1 19:25:51 CET 2014


Steven D'Aprano <steve+comp.lang.python at pearwood.info>:

> It seems to me that he's just assuming that symbols ought to be
> singletons, hence his focus on identity rather than equality.

Yes.

A practical angle is this: if I used strings as symbols and compared
them with "==", logically I shouldn't define them as constants but
simply use strings everywhere:

   class Connection:
       def __init__(self):
           self.state = "IDLE"

       def connect(self, address):
           if self.state == "IDLE":
               ...
           elif self.state == ...

The principal (practical) problem with that is that I might make a typo
and write:

           if self.state == "IDLE ":

which could result in some hard-to-find problems. That's why I want get
the help of the Python compiler and always refer to the states through
symbolic constants:

           if self.state == self.IDLE:


Marko



More information about the Python-list mailing list