Scoping issue with import

Discussion in 'Python' started by James Stroud, Feb 28, 2005.

  1. James Stroud

    James Stroud Guest

    Say I have a module, we'll call it "my_imported_mod". It contains a function
    in it that calls another function, "myfun". The "myfun" function is in the
    module "my_main_mod", that imports "my_imported_mod".

    The code of "my_main_mod" might look like this:
    ==============
    from my_imported_mod import *

    def myfun():
    print "stuff"
    ==============

    the code of "my_imported_mod" might look like this:
    ==============
    def somefun():
    myfun()
    ==============

    When trying to execute the function somefun(), I get a
    NameError: global name 'myfun' is not defined

    How to rectify this with minimal code change? How to let imported modules know
    about the namespace they are getting imported into? I do not want to
    restructure my code right now.

    Thanks in advance for help.

    James
    James Stroud, Feb 28, 2005
    #1
    1. Advertising

  2. James Stroud

    Carl Banks Guest

    James Stroud wrote:
    > Say I have a module, we'll call it "my_imported_mod". It contains a

    function
    > in it that calls another function, "myfun". The "myfun" function is

    in the
    > module "my_main_mod", that imports "my_imported_mod".


    [snip]

    > How to rectify this with minimal code change? How to let imported

    modules know
    > about the namespace they are getting imported into? I do not want to
    > restructure my code right now.
    >
    > Thanks in advance for help.



    Change it so that my_imported_mod imports my_main_mod. If A depends on
    B, then A should import B, not the other way around.

    If there's some good reason why B imports A despite this (and there
    could be--say if myfun is some kind of a callback), then you should
    probably pass myfun to the function in the imported module as an
    argument. If that's not practical, put myfun into a third module, and
    have my_imported_mod import that.

    If you want to live dangerously, you could do something like this from
    my_main_mod:

    import my_imported_mod
    my_imported_mod.myfun = myfun

    I don't recommend it, though, because my_imported_mod is no longer
    self-contained (i.e., it's a module not modular).


    --
    CARL BANKS
    Carl Banks, Mar 1, 2005
    #2
    1. Advertising

  3. James Stroud

    Steve Holden Guest

    James Stroud wrote:
    > Say I have a module, we'll call it "my_imported_mod". It contains a function
    > in it that calls another function, "myfun". The "myfun" function is in the
    > module "my_main_mod", that imports "my_imported_mod".
    >
    > The code of "my_main_mod" might look like this:
    > ==============
    > from my_imported_mod import *
    >
    > def myfun():
    > print "stuff"
    > ==============
    >
    > the code of "my_imported_mod" might look like this:
    > ==============
    > def somefun():
    > myfun()
    > ==============
    >
    > When trying to execute the function somefun(), I get a
    > NameError: global name 'myfun' is not defined
    >
    > How to rectify this with minimal code change? How to let imported modules know
    > about the namespace they are getting imported into? I do not want to
    > restructure my code right now.
    >
    > Thanks in advance for help.
    >
    > James


    You have had some good advice about avoiding circular imports.

    I just wanted you to consider the coupling of your module. If the called
    function is calling a function inside the module that's calling it, this
    is often a clue that the inner function should be passed as a parameter
    to the first one.

    Let me know if this isn't comprehensible and I'll give you an example,
    but to show this being done inside a single module I present the code below:

    >>> def caller(f, arg):

    ... return f(arg)
    ...
    >>> def call(summat):

    ... print summat * 3
    ...
    >>> caller(call, 21)

    63
    >>> caller(call, "string")

    stringstringstring

    This also demonstrates quite nicely how Python doesn't choose to
    discriminate between integers and strings at compile time, applying the
    correct definition of multiplication when the operation actually has to
    be performed.

    Some people hate this, most people who read comp.lang.python regularly
    will be quite happy to explain why it's a Good Thing (tm).

    regards
    Steve
    Steve Holden, Mar 1, 2005
    #3
    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. Frederick Grim

    scoping issue

    Frederick Grim, Sep 12, 2003, in forum: C++
    Replies:
    4
    Views:
    413
    Gianni Mariani
    Sep 14, 2003
  2. Pierre Rouleau
    Replies:
    4
    Views:
    798
    Pierre Rouleau
    Mar 7, 2004
  3. Replies:
    12
    Views:
    729
    Bo Persson
    Aug 15, 2007
  4. Elijah Newren
    Replies:
    1
    Views:
    199
    Peter Otten
    Mar 7, 2009
  5. John Nagle
    Replies:
    22
    Views:
    634
    Gregory Ewing
    Oct 16, 2010
Loading...

Share This Page