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

Steven Bethard steven.bethard at gmail.com
Tue Nov 22 14:19:15 EST 2005


Mardy wrote:
> I'm not sure I got your problem correctly, however see if this helps:
> 
> $ cat > test.py
> class myclass:
>     name = __module__
> ^D
>
[snip]
> 
> >>> import test
> >>> a = test.myclass()
> >>> a.name
> '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 
a.name 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.

STeVe

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