Adding to a module's __dict__?

Carl Banks pavlovevidence at gmail.com
Tue Mar 2 11:49:26 EST 2010


On Mar 2, 5:21 am, Roy Smith <r... at panix.com> wrote:
> In article <mailman.96.1267508316.23598.python-l... at python.org>,
>  Chris Rebert <c... at rebertia.com> wrote:
>
>
>
> > On Mon, Mar 1, 2010 at 8:27 PM, Roy Smith <r... at panix.com> wrote:
> > > >From inside a module, I want to add a key-value pair to the module's
> > > __dict__.  I know I can just do:
>
> > > FOO = 'bar'
>
> > > at the module top-level, but I've got 'FOO' as a string and what I
> > > really need to do is
>
> > > __dict__['Foo'] = 'bar'
>
> > > When I do that, I get "NameError: name '__dict__' is not defined".  Is
> > > it possible to do what I'm trying to do?
>
> > Yes; just modify the dict returned by the globals() built-in function
> > instead.
>
> Ah, cool.  Thanks.
>
> > It's usually not wise to do this and is better to use a
> > separate dict instead, but I'll assume you know what you're doing and
> > have good reasons to disregard the standard advice due to your
> > use-case.
>
> Why is it unwise?


He didn't say it was unwise.  He said it's usually not wise.


Carl Banks



More information about the Python-list mailing list