473,394 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

name of client module

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?
Feb 19 '08 #1
5 1553
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 (ni**@stinemates.org)
http://nick.stinemates.org

AIM: Nick Stinemates
MSN: ni************@hotmail.com
Yahoo: ni************@yahoo.com
==================
Feb 19 '08 #2
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.
Feb 19 '08 #3
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.
Feb 19 '08 #4
Nick Stinemates <ni**@stinemates.orgwrites:
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
Feb 19 '08 #5
Ben Finney wrote:
Nick Stinemates <ni**@stinemates.orgwrites:
>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.
Feb 19 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Glauco | last post by:
I'm trying to use SOAPpy 0.10.1 for a client but is difficult to handle easly Is this library in use or i'm using an OLD death library ? I'm alone in find a lot of problem in a SOAP Client ?...
3
by: George P | last post by:
I've run into a strange package related namespace problem. Follow these steps (on UNIX system) to illustrate: ------------------------- mkdir /tmp/mypkg cd /tmp/mypkg touch __init__.py echo...
15
by: Michael Rybak | last post by:
hi, everyone. I'm writing a 2-players game that should support network mode. I'm now testing it on 1 PC since I don't have 2. I directly use sockets, and both client and server do...
13
by: bobueland | last post by:
I'm a newbie experimenting with Python. I want to incrementally develop a module called 'circle'. The problem is now that the file name is used for two purposes. To keep track of the version number...
8
by: gel | last post by:
Hi I would like to pass a variable in and use it as part of a name of an object, something like below, where I pass the variable software into the function and use it as part of the name of the...
0
by: Johan P | last post by:
Having just compiled DBG 2.15.1 and trying to use it with PHP (cli) give some some problem. Verions: DBG 2.15.1 PHP 4.4.4 (cli) It works fine compiled with the Apache module but when I try...
1
by: alain MONTMORY | last post by:
Hello everybody, I am a newbie to python so I hope I am at the right place to expose my problem..... :-http://www.python.org/doc/2.4.2/ext/pure-embedding.html 5.3 Pure Embedding I download the...
2
by: DotNetGuy | last post by:
I have developed a data access layer in my web site application by using web client software factory and put it in my business module as a service. The question is: Is it a good option to reuse...
11
by: Bill Davy | last post by:
I am trying to edit Contacts in Outlook. This is so I can transfer numbers from my address book which is an Excel spreadsheet to my mobile phone. I came across the following snippet of code which...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.