imports after function definitions?

Discussion in 'Python' started by Hallvard B Furuseth, Aug 28, 2003.

  1. Is there any reason not to structure my program like this?

    def ...
    def ...
    var = ...
    var = ...
    import ...
    import ...
    main_function()

    E.g. does it compile to slower code, or does it confuse PyChecker or
    something, if I put the imports after the functions that use them?

    --
    Hallvard
     
    Hallvard B Furuseth, Aug 28, 2003
    #1
    1. Advertising

  2. Hallvard B Furuseth

    Terry Reedy Guest

    "Hallvard B Furuseth" <h.b.furuseth(nospam)@usit.uio(nospam).no> wrote
    in message news:...
    > Is there any reason not to structure my program like this?
    >
    > def ...
    > def ...
    > var = ...
    > var = ...
    > import ...
    > import ...
    > main_function()


    It is helpful to human readers to see dependencies at the top and to
    know what globals are avaiable when reading defs. What would be
    compensating gain of doing such?

    Terry J. Reedy
     
    Terry Reedy, Aug 29, 2003
    #2
    1. Advertising

  3. Terry Reedy wrote:
    >in message news:...
    >> Is there any reason not to structure my program like this?
    >>
    >> def ...
    >> def ...
    >> var = ...
    >> var = ...
    >> import ...
    >> import ...
    >> main_function()

    >
    > It is helpful to human readers to see dependencies at the top and to
    > know what globals are avaiable when reading defs. What would be
    > compensating gain of doing such?


    I'm setting sys.path in a config function which depends on variables
    near the end of the file. So the config function must be called late,
    and the imports that depend on it after that.

    --
    Hallvard
     
    Hallvard B Furuseth, Aug 29, 2003
    #3
  4. The only restriction is that if you want to reference some imported thing at
    the module level, then the import has to precede the use:

    import x
    class cls(x.cls1):
    pass

    Other than that I don't know of any reasons (other than readability, which
    can be interpreted to advantage either way).

    -- Rob --

    "Hallvard B Furuseth" <h.b.furuseth(nospam)@usit.uio(nospam).no> wrote in
    message news:...
    > Is there any reason not to structure my program like this?
    >
    > def ...
    > def ...
    > var = ...
    > var = ...
    > import ...
    > import ...
    > main_function()
    >
    > E.g. does it compile to slower code, or does it confuse PyChecker or
    > something, if I put the imports after the functions that use them?
    >
    > --
    > Hallvard
     
    Robert Dickinson, Aug 29, 2003
    #4
  5. Thanks for all the answers.

    Peter Hansen wrote:
    >Hallvard B Furuseth wrote:
    >> I'm setting sys.path in a config function which depends on variables
    >> near the end of the file. So the config function must be called late,
    >> and the imports that depend on it after that.

    >
    > Sounds like the structure ought to be changed (IMHO).


    OK, I did. Moved the sys.path setting out of the config function.

    > In other words, why do you need all those defines and all those
    > variables to come before the imports?


    I needed the config function to be called before the imports since
    it set sys.path, and the config function should be able to access
    some variables. So the imports had to come after it, and after
    those variables. I thought it looked better to have the imports
    at the end of the program than in the middle of it.

    Never mind, though. Now I have them right after the config function.
    I want that one at the beginning because that's what people need
    to edit.

    --
    Hallvard
     
    Hallvard B Furuseth, Sep 1, 2003
    #5
    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. Albert
    Replies:
    4
    Views:
    11,039
    Albert
    Jul 10, 2008
  2. Stef Mientki
    Replies:
    1
    Views:
    539
    Steven D'Aprano
    Oct 26, 2008
  3. Chris Colbert
    Replies:
    3
    Views:
    2,246
    Peter Otten
    Dec 10, 2009
  4. zildjohn01
    Replies:
    0
    Views:
    694
    zildjohn01
    Feb 22, 2011
  5. Victor Hooi
    Replies:
    1
    Views:
    130
    Devin Jeanpierre
    Nov 25, 2013
Loading...

Share This Page