Can global variable be passed into Python function?

Ben Finney ben+python at benfinney.id.au
Sat Mar 1 23:30:20 CET 2014


Marko Rauhamaa <marko at pacujo.net> writes:

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

Give that up, then. Your assumption is false in Python, and is not
needed to get the behaviour you say you need.

> 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

Yes, that works fine. It's also quite understandable for the reader.

> 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 just one of a huge variety of problems. Write a comprehensive
unit test suite to catch this and a great many other errors.

> That's why I want get the help of the Python compiler and always refer
> to the states through symbolic constants

Python doesn't let you compare symbols, only values. Work within its
constraints.

-- 
 \     “I cannot conceive that anybody will require multiplications at |
  `\   the rate of 40,000 or even 4,000 per hour …” —F. H. Wales, 1936 |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list