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

Discussion in 'Python' started by Johannes Janssen, Aug 9, 2009.

  1. 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
     
    Johannes Janssen, Aug 9, 2009
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sridhar R
    Replies:
    14
    Views:
    1,455
    =?iso-8859-1?Q?Fran=E7ois?= Pinard
    Feb 10, 2004
  2. Andreas Neudecker
    Replies:
    4
    Views:
    320
    Andreas Neudecker
    Aug 9, 2004
  3. Steven Bethard
    Replies:
    3
    Views:
    292
    Alex Martelli
    Nov 23, 2005
  4. Ami
    Replies:
    3
    Views:
    440
    Tomás
    Feb 27, 2006
  5. Martin P. Hellwig
    Replies:
    1
    Views:
    388
    Martin P. Hellwig
    Mar 26, 2010
Loading...

Share This Page