A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-list/2010-March/720291.html below:

Adding to a module's __dict__?

Adding to a module's __dict__? Adding to a module's __dict__?Jean-Michel Pichavant jeanmichel at sequans.com
Tue Mar 2 05:29:33 EST 2010
Roy Smith 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?
>   
test.py:

import sys
varName= 'foo'
setattr(sys.modules[__name__], varName, 42)



in a shell:
import test

print test.foo
 >>> 42


JM

More information about the Python-list mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4