How to find out in which module an instance of a class is created?

  • Thread starter Johannes Janssen
  • Start date
J

Johannes Janssen

Hi
I like to know in which module an instance of a class was initialized.
Using __module__ or __name__ within a class only gives me the module in
which the class was defined not the instance of the class. Is there some
(simple) way to do this?
For better understanding I'll give an example how I want to use this.
Okay let's say I've got a module *foo,* in which class A is defined as
the following:
> class A(object):
> def __init__(self, mod):
> self.mod = mod

In a module called *bar* I do the following:
> import foo
> a = A(__name__)

Then a.mod should be "bar". But I don't like to pass the value of a.mod
manually rather than having it default to the module the instance a of A
was created in (here "bar").
Unfortunately something like this ...
> class A(object):
> def __init__(self, mod=__name__):
> self.mod = mod

.... won't work. In this case mod would always be "foo".

Kind regards
johannes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,821
Messages
2,569,748
Members
45,726
Latest member
RaleighAll

Latest Threads

Top