On Thu, Nov 25, 2010 at 11:34 AM, Glenn Linderman <v+pyt...@g.nevcal.com> wrote:
> So the following code defines constants with associated names that get put
> in the repr.

The code you gave doesn't work if the constant() function is moved
into a separate module from the code that calls it.  The globals()
function, as I understand it, gives you access to the global namespace
*of the current module*, so the constants end up being defined in the
module containing constant(), not the module you're calling it from.

You could get around this by passing the globals of the calling module
to constant(), but I think it's cleaner to use a class to provide a
distinct namespace for the constants.

> An idea I had, but have no idea how to implement, is that it might be nice
> to say:
>
>     with imported_constants_from_module:
>            do_stuff
>
> where do_stuff could reference the constants without qualifying them by
> module.  Of course, if you knew it was just a module of constants, you could
> "import * from module" :)  But the idea of with is that they'd go away at
> the end of that scope.

I don't think this is possible - the context manager protocol doesn't
allow you to modify the namespace of the caller like that.  Also, a
with statement does not have its own namespace; any names defined
inside its body will continue to be visible in the containing scope.

Of course, if you want to achieve something similar (at function
scope), you could say:

def foo(bar, baz):
    from module import *
    ...
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to