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 6 1041
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
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( ) )
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
Arnaud Delobelle wrote:
Or:
import ModuleName as this_module
Or:
this_module = __import__(__name__)
then you don't have to change anything.
--
Greg
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
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 This discussion thread is closed Replies have been disabled for this discussion. Similar topics
9 posts
views
Thread by Penn Markham |
last post: by
|
6 posts
views
Thread by Krackers |
last post: by
|
4 posts
views
Thread by Richard Townsend |
last post: by
|
3 posts
views
Thread by Chris B. |
last post: by
|
reply
views
Thread by Alan Carpenter |
last post: by
|
4 posts
views
Thread by Keith G Hicks |
last post: by
|
2 posts
views
Thread by Anish Chapagain |
last post: by
|
1 post
views
Thread by Joe Strout |
last post: by
|
2 posts
views
Thread by Joe Strout |
last post: by
| | | | | | | | | | |