python gripes survey

Terry Reedy tjreedy at udel.edu
Wed Aug 27 21:44:44 EDT 2003


"Sean Ross" <sross at connectmail.carleton.ca> wrote in message
news:HE73b.2610$_F1.407684 at news20.bellglobal.com...

> This is probably not what you want, and it's probably worse than
just using
> a global declaration, but it *is* "another way to handle this":
>
> >>> # put this at the top of your module,
> >>> # to grab hold of this module's namespace
> >>> import __main__ as main
> >>> g = "global variable"
> >>> def f():
> ...  main.g = "re-bound global"
> ...
> >>> f()
> >>> g
> 're-bound global'

Unless there is flaw neither of us see, I like this.

Names bound in nested scopes are not read-only, just not rebindable.
Mutables *are* writeable and immutables can be wrapped in [] or
another mutable to make them effectively writable.

def fun():
   x=[1]
   print x
   def ny():
      x[0]=2
   ny()
   print x

>>> fun()
[1]
[2]

Slightly clumsy, but workable.

Terry J. Reedy






More information about the Python-list mailing list