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

Mardy mardy at users.sourceforge.net
Mon Nov 21 18:50:40 EST 2005


Hi Steven,

Le die Mon, 21 Nov 2005 11:37:37 -0700, Steven Bethard ha scribite:
[...]
> In the basic situation, where the instance is created in the same
module
> as the class, I can figure out 'mod' and 'name' like::
> 
>      cls = type(self)
>      name = cls.__module__
>      mod = __import__(cls.__module__)

I'm not sure I got your problem correctly, however see if this helps:

$ cat > test.py
class myclass:
    name = __module__
^D
$ python
Python 2.3.5 (#2, Jun 19 2005, 13:28:00)
[GCC 3.3.6 (Debian 1:3.3.6-6)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
>>> a = test.myclass()
>>> a.name
'test'

This works, as we define "name" to be a class attribute. 
Is this useful to you?


-- 
Saluti,
    Mardy
http://interlingua.altervista.org




More information about the Python-list mailing list