473,785 Members | 2,291 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function getting a reference to its own module

I have a function that needs a reference to the module object it is
defined in. (For the reason why, if you care, see the thread "doctest not
seeing any of my doc tests" from a week ago.) I know of two ways to deal
with this problem, both of which feel unsatisfactory to me. Assume the
name of the module is "Mod", then I can do either of these:

def foo():
import Mod
process(Mod)

Disadvantage: If I change the name of the module, I have to remember to
change the name of the module reference in foo() twice.
def foo():
modname = foo.__module__
module = __import__(modn ame)
process(module)

Disadvantage: if I change the name of the function, I have to remember to
change the reference to itself, but at least both changes are right next
to each other.

Assume that changing the function name or the module name are both
equally likely/unlikely.

Which do other people prefer? Which seems "better" to you? Are there any
other alternatives?

--
Steven
Sep 14 '08 #1
6 1116
On Sep 14, 10:29*am, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com .auwrote:
I have a function that needs a reference to the module object it is
defined in. (For the reason why, if you care, see the thread "doctest not
seeing any of my doc tests" from a week ago.) I know of two ways to deal
with this problem, both of which feel unsatisfactory to me. Assume the
name of the module is "Mod", then I can do either of these:

def foo():
* * import Mod
* * process(Mod)

Disadvantage: If I change the name of the module, I have to remember to
change the name of the module reference in foo() twice.

def foo():
* * modname = foo.__module__
* * module = __import__(modn ame)
* * process(module)

Disadvantage: if I change the name of the function, I have to remember to
change the reference to itself, but at least both changes are right next
to each other.

Assume that changing the function name or the module name are both
equally likely/unlikely.

Which do other people prefer? Which seems "better" to you? Are there any
other alternatives?
What about something like:

sys.modules[__name__] ?

--
Arnaud

Sep 14 '08 #2
On Sep 14, 4:43*am, Arnaud Delobelle <arno...@google mail.comwrote:
On Sep 14, 10:29*am, Steven D'Aprano <st...@REMOVE-THIS-

cybersource.com .auwrote:
I have a function that needs a reference to the module object it is
defined in. (For the reason why, if you care, see the thread "doctest not
seeing any of my doc tests" from a week ago.) I know of two ways to deal
with this problem, both of which feel unsatisfactory to me. Assume the
name of the module is "Mod", then I can do either of these:
def foo():
* * import Mod
* * process(Mod)
Disadvantage: If I change the name of the module, I have to remember to
change the name of the module reference in foo() twice.
def foo():
* * modname = foo.__module__
* * module = __import__(modn ame)
* * process(module)
Disadvantage: if I change the name of the function, I have to remember to
change the reference to itself, but at least both changes are right next
to each other.
Assume that changing the function name or the module name are both
equally likely/unlikely.
Which do other people prefer? Which seems "better" to you? Are there any
other alternatives?

What about something like:

* * sys.modules[__name__] ?

--
Arnaud
You're just worried about changing the module's name in the future.
So use a global variable or function that you only have to change
once.

def Mod_mod( ):
import Mod as Mod #<-- only one change needed
return Mod

def foo( ):
process( Mod_mod( ) )
Sep 14 '08 #3
On Sep 14, 5:10*pm, "Aaron \"Castironpi \" Brady"
<castiro...@gma il.comwrote:
On Sep 14, 4:43*am, Arnaud Delobelle <arno...@google mail.comwrote:
On Sep 14, 10:29*am, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com .auwrote:
I have a function that needs a reference to the module object it is
defined in. (For the reason why, if you care, see the thread "doctestnot
seeing any of my doc tests" from a week ago.) I know of two ways to deal
with this problem, both of which feel unsatisfactory to me. Assume the
name of the module is "Mod", then I can do either of these:
def foo():
* * import Mod
* * process(Mod)
Disadvantage: If I change the name of the module, I have to remember to
change the name of the module reference in foo() twice.
def foo():
* * modname = foo.__module__
* * module = __import__(modn ame)
* * process(module)
Disadvantage: if I change the name of the function, I have to remember to
change the reference to itself, but at least both changes are right next
to each other.
Assume that changing the function name or the module name are both
equally likely/unlikely.
Which do other people prefer? Which seems "better" to you? Are there any
other alternatives?
What about something like:
* * sys.modules[__name__] ?
--
Arnaud

You're just worried about changing the module's name in the future.
So use a global variable or function that you only have to change
once.

def Mod_mod( ):
* *import Mod as Mod #<-- only one change needed
* *return Mod

def foo( ):
* *process( Mod_mod( ) )
Or:

import ModuleName as this_module

def foo():
process(this_mo dule)

--
Arnaud

Sep 14 '08 #4
Arnaud Delobelle wrote:
Or:

import ModuleName as this_module
Or:

this_module = __import__(__na me__)

then you don't have to change anything.

--
Greg
Sep 17 '08 #5
On Wed, 17 Sep 2008 13:52:13 +1200, greg wrote:
Arnaud Delobelle wrote:
>Or:

import ModuleName as this_module

Or:

this_module = __import__(__na me__)

then you don't have to change anything.

I like that solution! And it works regardless of whether the module
holding it is imported, or is being executed from the commandline.

Thanks to everyone who made a suggestion.

--
Steven
Sep 17 '08 #6
On Sep 16, 10:29 pm, Steven D'Aprano
<ste...@REMOVE. THIS.cybersourc e.com.auwrote:
On Wed, 17 Sep 2008 13:52:13 +1200, greg wrote:
Arnaud Delobelle wrote:
Or:
import ModuleName as this_module
Or:
this_module = __import__(__na me__)
then you don't have to change anything.

I like that solution! And it works regardless of whether the module
holding it is imported, or is being executed from the commandline.
It doesn't work if the module is part of a package. For that could
use sys.modules[__name__] or write a separate function to return the
appropriate nested module of __name__. Neither method is foolproof I
don't think.
Carl Banks
Sep 17 '08 #7

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

Similar topics

9
4964
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my webserver runs that part of the script (see attached file, snippet.php), though, it doesn't go through. I don't get an error message or anything...it just returns a "1" (whereas it should return a "0") as far as I can tell. I have read the PHP...
6
14034
by: Krackers | last post by:
How do you write a function which returns a reference to an array. I can only get a function to return a copy of the array itself. I've had a look at some other threads in this group an the return value of a function acts like 'by Val' returning the value only (except for objects) can you make it return a reference instead? cheers, Krackers
4
1526
by: Richard Townsend | last post by:
I've been experimenting putting a reference to a function into a Queue object and was wondering what actually gets put in the Queue - is it the function's code object? If I read from the Queue in a different module, it appears that I don't need to import the module that defines the function - or any module that it uses - is this generally true, or are there some conditions to be aware of? The scenario I'm working on has child threads...
3
1396
by: Chris B. | last post by:
I'm getting some strange results in an asp.net web application that has recently gone live. In the header of almost all the pages, the full name of the currently logged in user is displayed. On occasion, this changes to the name of some other user currently on the site. Needless to say, some find this disconcerting. The routines that get and display the name are incredibly straight forward
0
1445
by: Alan Carpenter | last post by:
I'm curious about getting to the text of procedures in a database other than the current database without a new instance of Access. Note I don't want to execute or call anything, I just want to get to the text of each module. No Networks involved, just multiple drives on one local machine. <ramble> Win98, Access 8 I don't have the self discipline to keep track of all the incidental bits
4
1600
by: Keith G Hicks | last post by:
I'm an experienced foxpro, vba, delphi, sql programmer but this is something new to me. I'm creating a pretty simple vb.net exe (scantextfiles.exe) that will run on a server and read some text files into a database. But I also need to create a stand alone function that can be called by scantextfiles.exe. This stand alone function will use the MeasureString method to get the length of some of the text in the text files. I'm not putting that...
2
1156
by: Anish Chapagain | last post by:
Hi, I have Structure in C, program and the structure is being used with various function inside C coding but am getting undefined referenced to global method and few of them too uses the sturct module. my problem goes like this, ex.h ----------- #define NIL 0 /* Indicates ptr is nil */ #define NO_CODER 0 /* Means do not use an arithmetic
1
1683
by: Joe Strout | last post by:
I've been using docstring to exercise each of my modules, with code like this: def _test(): import doctest doctest.testmod() if __name__ == "__main__": _test()
2
1837
by: Joe Strout | last post by:
Some corrections, to highlight the depth of my confusion... On Nov 11, 2008, at 9:10 PM, Joe Strout wrote: Actually, it does not. And no, it isn't; it's the NAME of the module the function is in. I'm not sure what good that does me. docstring.testmod does take an
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10085
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,...
0
9947
tracyyun
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...
0
8968
agi2029
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...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6737
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();...
0
5379
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...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.