[Python-Dev] Re: let's not stretch a keyword's use unreasonably, _please_...

Terry Reedy tjreedy at udel.edu
Thu Oct 23 01:51:45 EDT 2003


"Alex Martelli" <aleaxit at yahoo.com> wrote in message
news:20031022161137.96353.qmail at web40513.mail.yahoo.com...
> Inside a module M's body ("toplevel" in it, not nested inside
> a def &c) I can write
>     x = 23
> and it means M.x = 23 (unconditionally).  Once the module
> object M is created, if I want to tweak that attribute
> of M I have to write e.g. M.x = 42 after getting ahold of
> some reference to M (say by an "import M", or say in a function
> of M by sys.modules[__name__].x = 42, etc).
> Inside a module M's body ("toplevel" in it, not nested inside
> a def &c) I can write
>     x = 23
> and it means M.x = 23 (unconditionally).  Once the module
> object M is created, if I want to tweak that attribute
> of M I have to write e.g. M.x = 42 after getting ahold of
> some reference to M (say by an "import M", or say in a function
> of M by sys.modules[__name__].x = 42, etc).

I somehow overlooked that this would work inside modules also.

>>> import __main__ as m # I know, not general, just for trial
>>> m.c=3
>>> c
3
>>> def e():
...   m.x='ha'
...
>>> e()
>>> x
'ha'

So I really *don't* need global.  Perhaps a new builtin

def me():
  import sys
  return sys.modules[__name__]

or an addition to my template.py file.

Terry J. Reedy






More information about the Python-Dev mailing list