__module__ (was Re: Deprecate self)

Tim Hochberg tim.hochberg at ieee.org
Fri Apr 20 18:45:40 EDT 2001


"Alex Martelli" <aleaxit at yahoo.com> wrote in message
news:9bq9ir01lad at news1.newsguy.com...

[SNAP]

> If modifying globals from inside a function is meant to be a rare
> thing, then I agree with you that using "__module__.x=y" &c for
> such modifications (rather than "global x" followed by "x=y") may
> be perfectly acceptable.  The global statement does cater better
> for a style in which such "rebinding of globals" from within a
> function come "in clusters" -- many functions may not need to
> do any such binding, but those that do may need to bind more
> than one global or at least bind a global in several places, e.g.
>
> def example():
>     global x,y,z
>     if x>0:
>         x=y=z=23
>     elif y<z:
>         x=y=z=34
>
> is already substantially more readable than an equivalent
> using __module__ six times.

In this case, might I suggest the following (hypothetical) idiom:

def example2():
   m = __module__
   if x > 0:
      m.x = m.y = m.z = 23
   elif y < z:
      m.x = m.y = m.z = 34

[SNAP]

-tim





More information about the Python-list mailing list