[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

name of client module

Jeff Schwab

2/19/2008 12:43:00 AM

Q1: When a module is imported, is there any way for the module to
determine the name of the client code's module?

Q2: My understanding is that the code in a module is executed only on
the first import of that module. Is there any way to have a hook
invoked on subsequent imports, and for that hook (as in Q1) to determine
the name of the client module?
25 Answers

Nick Stinemates

2/19/2008 2:16:00 AM

0

Jeff Schwab wrote:
> Q1: When a module is imported, is there any way for the module to
> determine the name of the client code's module?
>
Why would you ever want to do this?
> Q2: My understanding is that the code in a module is executed only on
> the first import of that module. Is there any way to have a hook
> invoked on subsequent imports, and for that hook (as in Q1) to determine
> the name of the client module?
>
Why would you ever want to do this?

I don't really understand why you wouldn't want to do the following:

import foo
foo.exec()



--
==================
Nick Stinemates (nick@stinemates.org)
http://nick.stin...

AIM: Nick Stinemates
MSN: nickstinemates@hotmail.com
Yahoo: nickstinemates@yahoo.com
==================


Jeff Schwab

2/19/2008 2:35:00 AM

0

Nick Stinemates wrote:
> Jeff Schwab wrote:
>> Q1: When a module is imported, is there any way for the module to
>> determine the name of the client code's module?
>>
> Why would you ever want to do this?
>> Q2: My understanding is that the code in a module is executed only on
>> the first import of that module. Is there any way to have a hook
>> invoked on subsequent imports, and for that hook (as in Q1) to determine
>> the name of the client module?
>>
> Why would you ever want to do this?

So that the imported module can implement functions that return
information about the client module, as a form of introspection.
Suppose I want to know whether I'm the main module, and I don't want to
write __name__ == '__main__'; it would be nice if I could import a
module and call a method to tell me whether I'm __main__:

import modinfo

if modinfo.main():
print("Hello, world")

> I don't really understand why you wouldn't want to do the following:
>
> import foo
> foo.exec()

I'm not saying I don't want to do that. I'm saying that, in addition to
what you've written, I want foo to know it's being imported, and by whom.

Nick Stinemates

2/19/2008 2:52:00 AM

0


> I'm not saying I don't want to do that. I'm saying that, in addition to
> what you've written, I want foo to know it's being imported, and by whom.
>

You're still not explaining a real example of what this could be used for.

Oh well, here's an example of an implementation of what you want to do.

Import.py
================
#!/usr/bin/python

class Importer:
def __init__(self):
pass
def __import__(self, module):
exec "import %s" % module
exec "a = %s" % module
a.setImported(self)

i = Importer()
i.__import__("Imported")
================

Imported.py
================
#!/usr/bin/python

def setImported(importer):
print "I've been imported by %s" %importer
================


--
==================
Nick Stinemates (nick@stinemates.org)
http://nick.stin...

AIM: Nick Stinemates
MSN: nickstinemates@hotmail.com
Yahoo: nickstinemates@yahoo.com
==================


Jeff Schwab

2/19/2008 3:10:00 AM

0

Nick Stinemates wrote:
>> I'm not saying I don't want to do that. I'm saying that, in addition to
>> what you've written, I want foo to know it's being imported, and by whom.

Please don't snip so much.

> You're still not explaining a real example of what this could be used for.

Why would you say something like that? I told you *exactly* what I
wanted to use it for. See Berwyn's post on the recent thread "Double
underscores -- ugly?" He suggests that a suitable replacement for "if
__name__ == '__main__'" might be "if sys.main()". I was looking for a
way to implement that kind of function. Just like I told you when you
asked me. You snipped it, along with most of the post.

> Oh well, here's an example of an implementation of what you want to do.

Thanks.

Nick Stinemates

2/19/2008 3:20:00 AM

0

Jeff Schwab wrote:
> Nick Stinemates wrote:
>
>>> I'm not saying I don't want to do that. I'm saying that, in addition to
>>> what you've written, I want foo to know it's being imported, and by whom.
>>>
>
> Please don't snip so much.
>
>
>> You're still not explaining a real example of what this could be used for.
>>
>
> Why would you say something like that? I told you *exactly* what I
> wanted to use it for. See Berwyn's post on the recent thread "Double
> underscores -- ugly?" He suggests that a suitable replacement for "if
> __name__ == '__main__'" might be "if sys.main()". I was looking for a
> way to implement that kind of function. Just like I told you when you
> asked me. You snipped it, along with most of the post.
>
>
>> Oh well, here's an example of an implementation of what you want to do.
>>
>
> Thanks.
>
Ah, I snipped because I was only replying to that specific part and
thought there was an archive of the rest. If that is unconventional I'll
stop.

I suppose I did get a bit carried away. It seems people always want to
know 'who is calling my object' but I think writing a design around that is:

a) Really hard to follow, much like GoTo statements
b) Poorly thought out
and, probably much more importantly
c) Grouping code together like that really limits what you can do
with it..

I'm sorry if I offended.

--
==================
Nick Stinemates (nick@stinemates.org)
http://nick.stin...

AIM: Nick Stinemates
MSN: nickstinemates@hotmail.com
Yahoo: nickstinemates@yahoo.com
==================


Jeff Schwab

2/19/2008 3:22:00 AM

0

Nick Stinemates wrote:
> Jeff Schwab wrote:
>> Nick Stinemates wrote:
>>
>>>> I'm not saying I don't want to do that. I'm saying that, in addition to
>>>> what you've written, I want foo to know it's being imported, and by whom.
>>>>
>> Please don't snip so much.
>>
>>
>>> You're still not explaining a real example of what this could be used for.
>>>
>> Why would you say something like that? I told you *exactly* what I
>> wanted to use it for. See Berwyn's post on the recent thread "Double
>> underscores -- ugly?" He suggests that a suitable replacement for "if
>> __name__ == '__main__'" might be "if sys.main()". I was looking for a
>> way to implement that kind of function. Just like I told you when you
>> asked me. You snipped it, along with most of the post.
>>
>>
>>> Oh well, here's an example of an implementation of what you want to do.
>>>
>> Thanks.
>>
> Ah, I snipped because I was only replying to that specific part and
> thought there was an archive of the rest. If that is unconventional I'll
> stop.
>
> I suppose I did get a bit carried away. It seems people always want to
> know 'who is calling my object' but I think writing a design around that is:
>
> a) Really hard to follow, much like GoTo statements
> b) Poorly thought out
> and, probably much more importantly
> c) Grouping code together like that really limits what you can do
> with it..
>
> I'm sorry if I offended.

No problem! In general, I agree with those points completely.

Ben Finney

2/19/2008 5:40:00 AM

0

Nick Stinemates <nick@stinemates.org> writes:

> Ah, I snipped because I was only replying to that specific part and
> thought there was an archive of the rest. If that is unconventional
> I'll stop.

Please continue snipping the parts that aren't relevant to your reply.
The convention in this forum is trimmed-quote, inline-reply.

I think the complaint in this case might have been the "you snipped
bits that *were* relevant" failure mode :-)

--
\ "Madness is rare in individuals, but in groups, parties, |
`\ nations and ages it is the rule." -- Friedrich Nietzsche |
_o__) |
Ben Finney

Jeff Schwab

2/19/2008 5:41:00 AM

0

Ben Finney wrote:
> Nick Stinemates <nick@stinemates.org> writes:
>
>> Ah, I snipped because I was only replying to that specific part and
>> thought there was an archive of the rest. If that is unconventional
>> I'll stop.
>
> Please continue snipping the parts that aren't relevant to your reply.
> The convention in this forum is trimmed-quote, inline-reply.
>
> I think the complaint in this case might have been the "you snipped
> bits that *were* relevant" failure mode :-)

What he said.

Michael Ragland

7/24/2010 3:30:00 PM

0

On Jul 24, 9:11 am, Ben Cramer <bencram...@gmail.com> wrote:
> On Jul 24, 6:26 pm, hol...@mappi.helsinki.fi (Eugene Holman) wrote:
>
> > In article
> > <c6ecc327-e2d9-4668-ae08-fcd017606...@x18g2000pro.googlegroups.com>, Ben
>
> > Cramer <bencram...@gmail.com> wrote:
> > > I note the Ragland infestation has returned and is spamming the group
> > > with his Holocaust marketing propaganda. His only supporter is
> > > Holmanstein. Why does that not come as any surprise to me?
>
> > > It's fitting that the loons have found each other.
>
> > Ben, here's a bit of friendly advice. Instead of criticizing the
> > activities of newsgroup members who are doing precisely what the newsgroup
> > [alt.revisionism] was intended for, that is to say, serve as a forum for
> > open discussion of and exchange of opinion on the Holocaust and Holocaust
> > denial,
>
> It was never intended to be a forum dedicated to Holocaust(tm)
> marketing, holmanstein.
>
> > why don't you return to grammar school and learn to spell
> > 'circle'.
>
> Dear, oh dear. Nothing else in your arsenal, holmanstein?
>
> I was actually pulling Ragland's pisser. He has obviously not received
> any education beyond grade school.

I don't think a higher education is necessarily required to discuss/
debate the Holocaust. Ever since I was in my teens and I'm now 44 I've
read numerous books on the Holocaust, watched documentaries of it,
seen movies about it, etc. I've also read Holocaust denial/antisemitic
literature on the internet and assorted hate websites. You are
ignorant if you think an education necessarily makes somebody
intelligent. You are also ignorant if you think an education makes
somebody a good person. I'm not knocking higher education. It does
increase one's knowledge. Several anti-revisionists on here are
educated. They have attended universities and have degrees. I'm not
one of them. If you want to hold that against me then be my guest.

Michael Ragland

Michael Ragland

7/24/2010 4:32:00 PM

0

On Jul 24, 12:18 pm, "Jason P" <jas...@invalid.com> wrote:
> "Michael Ragland" <michaeleugeneragl...@gmail.com> wrote in message
>
> news:4855df3d-de4a-4290-8c03-05dfbd1241de@e5g2000yqn.googlegroups.com...
> On Jul 24, 9:11 am, Ben Cramer <bencram...@gmail.com> wrote:
>
>
>
> > On Jul 24, 6:26 pm, hol...@mappi.helsinki.fi (Eugene Holman) wrote:
>
> > > In article
> > > <c6ecc327-e2d9-4668-ae08-fcd017606...@x18g2000pro.googlegroups.com>, Ben
>
> > > Cramer <bencram...@gmail.com> wrote:
> > > > I note the Ragland infestation has returned and is spamming the group
> > > > with his Holocaust marketing propaganda. His only supporter is
> > > > Holmanstein. Why does that not come as any surprise to me?
>
> > > > It's fitting that the loons have found each other.
>
> > > Ben, here's a bit of friendly advice. Instead of criticizing the
> > > activities of newsgroup members who are doing precisely what the
> > > newsgroup
> > > [alt.revisionism] was intended for, that is to say, serve as a forum for
> > > open discussion of and exchange of opinion on the Holocaust and
> > > Holocaust
> > > denial,
>
> > It was never intended to be a forum dedicated to Holocaust(tm)
> > marketing, holmanstein.
>
> > > why don't you return to grammar school and learn to spell
> > > 'circle'.
>
> > Dear, oh dear. Nothing else in your arsenal, holmanstein?
>
> > I was actually pulling Ragland's pisser. He has obviously not received
> > any education beyond grade school.
>
> ------------
>
> >I don't think a higher education  is necessarily required to discuss/
> >debate the Holocaust. Ever since I was in my teens and I'm now 44 I've
> >read numerous books on the Holocaust, watched documentaries of it,
> >seen movies about it, etc. I've also read Holocaust denial/antisemitic
> >literature on the internet and assorted hate websites. You are
> >ignorant if you think an education necessarily makes somebody
> >intelligent. You are also ignorant if you think an education makes
> >somebody a good person. I'm not knocking higher education. It does
> >increase one's knowledge. Several anti-revisionists on here are
> >educated. They have attended universities and have degrees. I'm not
> >one of them. If you want to hold that against me then be my guest.
>
> You're an idiot!
>
> Michael Ragland

Why do you say that Jason P?