Function getting a reference to its own module

S

Steven D'Aprano

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?
 
A

Arnaud Delobelle

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__] ?
 
A

Aaron \Castironpi\ Brady

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__] ?

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( ) )
 
A

Arnaud Delobelle

What about something like:
    sys.modules[__name__] ?

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)
 
S

Steven D'Aprano

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.
 
C

Carl Banks

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top