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? 5 1583
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**@stinemate s.org) http://nick.stinemates.org
AIM: Nick Stinemates
MSN: ni************@ hotmail.com
Yahoo: ni************@ yahoo.com
=============== ===
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 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 <ni**@stinemate s.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
Ben Finney wrote:
Nick Stinemates <ni**@stinemate s.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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 ?
I'm going crazy because function are not documented .
Exist another solution for di a SOAP CLient ??
|
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 'import os\ndef test():\n print os.getcwd()' > os.py
cd /tmp
|
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 computations, the only data transfered is
user mouse/kbd input.
It works synchronously, but somehow, when I play in client window,
both client and server have 17 fps, while when playing in server
window, server has 44 fps while client ...
|
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 and as the name
for the module. So when I develop the first version of my file I have
to call it circle_a.py. The name of the module then automatically
becomes circle_a. But when I develop the next increment and call my
file circle_b.py the...
|
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 object so that I can
append to it using the other vairables. Any suggestions?
def a_d(self,software,mac,time,extra):
global d_software
d_software.software.append()
| |
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 to use it with
the client I get the following warnings trying to run the client version of
PHP.
|
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 code example from
http://www.python.org/doc/2.4.2/ext/run-func.txt
I call the file "TestOfficiel.c" and I compile it with :
gcc -g -I/usr/include/python2.3/ TestOfficiel.c -o TestOfficiel
-lpython2.3 -ldl
all is OK (or seems to be...).
|
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 this data access layer in my web services
developed with Web service software factory to remove the rudundant code?
What's the best approach to create a data access layer to be used in (web
client software factory ) web site and (web service...
|
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 enabled me to the contacts
at least list. I had to root around to discover CdoDefaultFolderContacts
(though it was guessable; how could I enumerate win32com.client.constants?).
I now want to work through the Contacts in Outlook patching in...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |