[Python-Dev] constant/enum type in stdlib

Michael Foord fuzzyman at voidspace.org.uk
Thu Nov 25 11:34:25 CET 2010


On 25/11/2010 10:12, Nadeem Vawda wrote:
> On Thu, Nov 25, 2010 at 11:34 AM, Glenn Linderman<v+python at 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 *
>      ...

Not in Python 3 you can't. :-)

That's invalid syntax, import * can only be used at module level. This 
makes *testing* import * (i.e. testing your __all__) annoying - you have 
to exec('from module import *') instead.

Michael

> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: http://mail.python.org/mailman/options/python-dev/fuzzyman%40voidspace.org.uk


-- 

http://www.voidspace.org.uk/

READ CAREFULLY. By accepting and reading this email you agree,
on behalf of your employer, to release me from all obligations
and waivers arising from any and all NON-NEGOTIATED agreements,
licenses, terms-of-service, shrinkwrap, clickwrap, browsewrap,
confidentiality, non-disclosure, non-compete and acceptable use
policies (”BOGUS AGREEMENTS”) that I have entered into with your
employer, its partners, licensors, agents and assigns, in
perpetuity, without prejudice to my ongoing rights and privileges.
You further represent that you have the authority to release me
from any BOGUS AGREEMENTS on behalf of your employer.



More information about the Python-Dev mailing list