473,324 Members | 2,501 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,324 software developers and data experts.

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__(modname)
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 1096
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__(modname)
* * 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...@googlemail.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__(modname)
* * 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...@gmail.comwrote:
On Sep 14, 4:43*am, Arnaud Delobelle <arno...@googlemail.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__(modname)
* * 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_module)

--
Arnaud

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

import ModuleName as this_module
Or:

this_module = __import__(__name__)

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__(__name__)

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.cybersource.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__(__name__)
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
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...
6
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...
4
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...
3
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...
0
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...
4
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...
2
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...
1
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
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.