[Tutor] get a module's own (top_level) dict?

Kent Johnson kent37 at tds.net
Sun Nov 9 13:41:13 CET 2008


On Sun, Nov 9, 2008 at 5:34 AM, spir <denis.spir at free.fr> wrote:
> Hello pyhonistas,
>
> Example:
> === module content ===
> a = 1
> b = 2
> ======================
>
> I'm looking for a way to get something like {'a':a, b':2}. 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

>From within the module, globals() returns the dict you want. However I
would say that needing to know the name of something is a code smell.
The usual way to do this is to keep an explicit dict of the objects of
interest.

Why do you need objects to know their name? Why can't you tell it it's
name when you create it?

Kent


More information about the Tutor mailing list