Can global variable be passed into Python function?
marko at pacujo.net
Sat Mar 1 11:39:58 CET 2014
Steven D'Aprano <steve+comp.lang.python at pearwood.info>:
> But I'm not sure that there is a good reason to put the class definitions
> inside the __init__ method. That means every time you create a new
> StateMachine instance, the classes have to be re-created.
> It will be much more efficient if you pull all the Idle, etc. classes out
> and make them top-level global classes.
> But then the closure over sm won't work...
As for effiency, I don't know if it is more efficient to create ad hoc
classes or ad hoc instances; might be a tossup in CPython.
> Since in this example you've actually got significant behaviour in the
> states, they aren't just symbols, and some sort of solution along
> these lines (whether you use multiple inner classes or not) is
That inner class solution is an alternative to the symbol approach. In
the absense of a switch statement, the inner class has lots going for
More information about the Python-list