By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,890 Members | 1,273 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,890 IT Pros & Developers. It's quick & easy.

imports after function definitions?

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"Hallvard B Furuseth" <h.b.furuseth(nospam)@usit.uio(nospam).no> wrote
in message news:HB**************@bombur.uio.no...
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
Jul 18 '05 #2

P: n/a
Terry Reedy wrote:
in message news:HB**************@bombur.uio.no...
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
Jul 18 '05 #3

P: n/a
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:HB**************@bombur.uio.no...
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

Jul 18 '05 #4

P: n/a
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
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.