Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
How to find out in which module an instance of a class is created?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Johannes Janssen  
View profile  
 More options Aug 9 2009, 6:06 pm
Newsgroups: comp.lang.python
From: Johannes Janssen <m...@johannes-janssen.de>
Date: Mon, 10 Aug 2009 00:06:44 +0200
Local: Sun, Aug 9 2009 6:06 pm
Subject: How to find out in which module an instance of a class is created?
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Christian Heimes  
View profile  
 More options Aug 9 2009, 7:47 pm
Newsgroups: comp.lang.python
From: Christian Heimes <li...@cheimes.de>
Date: Mon, 10 Aug 2009 01:47:34 +0200
Local: Sun, Aug 9 2009 7:47 pm
Subject: Re: How to find out in which module an instance of a class is created?

Johannes Janssen wrote:
>  > class A(object):
>  >     def __init__(self, mod=__name__):
>  >         self.mod = mod

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

You have to inspect the stack in order to get the module of the caller.
The implementation of warnings.warn() gives you some examples how to get
the module name.

Christian


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Johannes Janssen  
View profile  
 More options Aug 10 2009, 5:51 pm
Newsgroups: comp.lang.python
From: Johannes Janssen <m...@johannes-janssen.de>
Date: Mon, 10 Aug 2009 23:51:59 +0200
Local: Mon, Aug 10 2009 5:51 pm
Subject: Re: How to find out in which module an instance of a class is created?
Christian Heimes schrieb:
> Johannes Janssen wrote:
>>  > class A(object):
>>  >     def __init__(self, mod=__name__):
>>  >         self.mod = mod

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

> You have to inspect the stack in order to get the module of the
> caller. The implementation of warnings.warn() gives you some examples
> how to get the module name.

> Christian

Thanks for the quick and very helpful reply. Basically just copying from
warnings.warn(), I came up with this:

import sys

class A(object):
    def __init__(self, mod=None):
        if mod is None:
            self.mod = sys._getframe(1).f_globals['__name__']
        else:
            self.mod = mod

In warnings.warn() they used try around sys._getframe(1). As far as I
understand what is done in warnings, there it is not sure what object
caused the warning and therefore it is not sure whether you can or
cannot use sys._getframe(1). Though in my case it should be quite clear.
Can I be sure that my code will always work?

Johannes


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gabriel Genellina  
View profile  
 More options Aug 13 2009, 2:16 am
Newsgroups: comp.lang.python
From: "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
Date: Thu, 13 Aug 2009 03:16:15 -0300
Local: Thurs, Aug 13 2009 2:16 am
Subject: Re: How to find out in which module an instance of a class is created?
En Mon, 10 Aug 2009 18:51:59 -0300, Johannes Janssen  
<m...@johannes-janssen.de> escribi :

> class A(object):
>     def __init__(self, mod=None):
>         if mod is None:
>             self.mod = sys._getframe(1).f_globals['__name__']
>         else:
>             self.mod = mod

> In warnings.warn() they used try around sys._getframe(1). As far as I  
> understand what is done in warnings, there it is not sure what object  
> caused the warning and therefore it is not sure whether you can or  
> cannot use sys._getframe(1). Though in my case it should be quite clear.  
> Can I be sure that my code will always work?

The try/except around sys._getframe(1) is because that function is not  
mandatory/available on all Python implementations (that's the case for  
jython which doesn't provide it).

--
Gabriel Genellina


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Johannes Janssen  
View profile  
 More options Aug 16 2009, 6:25 am
Newsgroups: comp.lang.python
From: Johannes Janssen <m...@johannes-janssen.de>
Date: Sun, 16 Aug 2009 12:25:46 +0200
Local: Sun, Aug 16 2009 6:25 am
Subject: Re: How to find out in which module an instance of a class is created?
Gabriel Genellina schrieb:

> The try/except around sys._getframe(1) is because that function is not
> mandatory/available on all Python implementations (that's the case for
> jython which doesn't provide it).

Thanks, shouldn't such information be part of the python documentation
of sys._getframe()
(http://docs.python.org/library/sys.html?highlight=sys._getframe#sys._...

Johannes


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Rebert  
View profile  
 More options Aug 16 2009, 6:30 am
Newsgroups: comp.lang.python
From: Chris Rebert <c...@rebertia.com>
Date: Sun, 16 Aug 2009 06:30:54 -0400
Local: Sun, Aug 16 2009 6:30 am
Subject: Re: How to find out in which module an instance of a class is created?
On Sun, Aug 16, 2009 at 6:25 AM, Johannes

Janssen<m...@johannes-janssen.de> wrote:
> Gabriel Genellina schrieb:

>> The try/except around sys._getframe(1) is because that function is not
>> mandatory/available on all Python implementations (that's the case for
>> jython which doesn't provide it).

> Thanks, shouldn't such information be part of the python documentation of
> sys._getframe()
> (http://docs.python.org/library/sys.html?highlight=sys._getframe#sys._...

The leading underscore kinda indirectly implies it, but yeah, it's
worth mentioning.
File a bug in the docs: http://bugs.python.org/

Cheers,
Chris
--
http://blog.rebertia.com


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Johannes Janssen  
View profile  
 More options Aug 16 2009, 6:50 am
Newsgroups: comp.lang.python
From: Johannes Janssen <m...@johannes-janssen.de>
Date: Sun, 16 Aug 2009 12:50:27 +0200
Local: Sun, Aug 16 2009 6:50 am
Subject: Re: How to find out in which module an instance of a class is created?
Chris Rebert schrieb:

I filed a bug: http://bugs.python.org/issue6712 .

Johannes


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »