On 7/3/06, BJörn Lindqvist <[EMAIL PROTECTED]> wrote:
> On 7/1/06, Guido van Rossum <[EMAIL PROTECTED]> wrote:
> > On 6/30/06, Steven Bethard <[EMAIL PROTECTED]> wrote:
> > > BJörn Lindqvist wrote:
> > > > I have often wanted something similar to that for global
> > > > variables, instead of the global declaration:
> > > >
> > > > cache = None
> > > > def init():
> > > >     if not global.cache:
> > > >         global.cache = init_cache()
> > >
> > > Redirected since this seemed like a Python 3000 kind of request.  I
> > > like the idea, particularly because it coincides well with my usual
> > > uses for global/globals().  Seems like it might require some changes
> > > in things like eval and exec that take locals and globals dicts, but I
> > > don't know how much of a drawback that is.
> >
> > You realize that *reading* a global doesn't need the "global." prefix,
> > do you? So you could have written "if not cache: global.cache =
> > init_cache()" in the function body.
> >
> > I'm not sure I like this asymmetry much.
>
> I think the fix for that is to remove the "scope inheritance." I.e:
>
> cache = None
> def init():
>     if not cache:
>         pass
>
> Throws a NameError because cache is not declared in function init's
> scope. So you would be forced to write:
>
> cache = None
> def init():
>     if not global.cache:
>         global.cache = "foobar"
>
> I like the symmetry with self in classes. YMMV

That can't work because imported names are also globals, as are
classes and functions defined in the same module.

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)
_______________________________________________
Python-3000 mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-3000
Unsubscribe: 
http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com

Reply via email to