Function getting a reference to its own module

Discussion in 'Python' started by Steven D'Aprano, Sep 14, 2008.

  1. 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
    Steven D'Aprano, Sep 14, 2008
    #1
    1. Advertising

  2. On Sep 14, 10:29 am, Steven D'Aprano <st...@REMOVE-THIS-
    cybersource.com.au> wrote:
    > 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
    Arnaud Delobelle, Sep 14, 2008
    #2
    1. Advertising

  3. On Sep 14, 4:43 am, Arnaud Delobelle <> wrote:
    > On Sep 14, 10:29 am, Steven D'Aprano <st...@REMOVE-THIS-
    >
    >
    >
    > cybersource.com.au> wrote:
    > > 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( ) )
    Aaron \Castironpi\ Brady, Sep 14, 2008
    #3
  4. On Sep 14, 5:10 pm, "Aaron \"Castironpi\" Brady"
    <> wrote:
    > On Sep 14, 4:43 am, Arnaud Delobelle <> wrote:
    >
    >
    >
    > > On Sep 14, 10:29 am, Steven D'Aprano <st...@REMOVE-THIS-

    >
    > > cybersource.com.au> wrote:
    > > > 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( ) )


    Or:

    import ModuleName as this_module

    def foo():
    process(this_module)

    --
    Arnaud
    Arnaud Delobelle, Sep 14, 2008
    #4
  5. Steven D'Aprano

    greg Guest

    Arnaud Delobelle wrote:

    > Or:
    >
    > import ModuleName as this_module


    Or:

    this_module = __import__(__name__)

    then you don't have to change anything.

    --
    Greg
    greg, Sep 17, 2008
    #5
  6. 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
    Steven D'Aprano, Sep 17, 2008
    #6
  7. Steven D'Aprano

    Carl Banks Guest

    On Sep 16, 10:29 pm, Steven D'Aprano
    <> wrote:
    > 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
    Carl Banks, Sep 17, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Saverio M.
    Replies:
    0
    Views:
    496
    Saverio M.
    Jul 3, 2006
  2. thunk
    Replies:
    1
    Views:
    286
    thunk
    Mar 30, 2010
  3. thunk
    Replies:
    0
    Views:
    444
    thunk
    Apr 1, 2010
  4. thunk
    Replies:
    14
    Views:
    588
    thunk
    Apr 3, 2010
  5. David Filmer
    Replies:
    17
    Views:
    246
    J. Romano
    Aug 18, 2004
Loading...

Share This Page