loop performance in global namespace (python-2.6.1)

Scott David Daniels Scott.Daniels at Acm.Org
Thu Mar 12 19:53:58 CET 2009


Poor Yorick wrote:
> In the following snippet, the loop in the global namespace takes twice 
> as long as the loop in the function namespace.  Why?

Locals are known to have no outside interaction, and so are not looked
up by name.  your code could have a thread that did,
....
      global counter
      while True:
          time.sleep(.1)
          counter *= 2
....

For that matter, it could do ... del counter ... and force a NameError
in your second loop.

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list