the name of a module in which an instance is created?

Steven Bethard steven.bethard at
Tue Nov 22 20:19:15 CET 2005

Mardy wrote:
> I'm not sure I got your problem correctly, however see if this helps:
> $ cat >
> class myclass:
>     name = __module__
> ^D
> >>> import test
> >>> a = test.myclass()
> >>>
> 'test'
> This works, as we define "name" to be a class attribute. 
> Is this useful to you?

Unfortunately, no, this is basically what I currently have.  Instead of printing 'test', it should print '__main__'.  I want the name of 
the module in which the *instance* is created, not the name of the 
module in which the *class* is created.


P.S. Note that I already discussed two possible solutions which I didn't 
much like: (1) pass __name__ to the class instance, e.g. ``a = 
test.myclass(__name__)`` or (2) declare an empty sublcass of myclass in 
the second module (in your case, at the interactive prompt).

More information about the Python-list mailing list