[Tutor] get a module's own (top_level) dict?
Emile van Sebille
emile at fenx.com
Wed Nov 12 21:20:25 CET 2008
spir wrote:
> Hello pyhonistas,
>
> Example:
> === module content ===
> a = 1
> b = 2
> ======================
>
> I'm looking for a way to get something like {'a':a, b':2}.
Maybe this will get you started:
[root at vsds2 root]# cat > testmod.py
a=1
b=2
[root at vsds2 root]# python
Python 2.5 (r25:51908, Nov 1 2006, 15:36:22)
[GCC 2.96 20000731 (Red Hat Linux 7.1 2.96-98)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import testmod
>>> dir(testmod)
['__builtins__', '__doc__', '__file__', '__name__', 'a', 'b']
>>> D=dict([(varname,getattr(testmod,varname))
for varname in dir(testmod)
if not varname.startswith("_") ])
>>> D
{'a': 1, 'b': 2}
>>>
HTH,
Emile
> Actually,
> names defind in the module will be instances of a custom type. I want to
> give them an attribute that holds their own name. E.g.:
> for key,obj in dict:
> obj.name = key
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list