Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > imports after function definitions?

Reply
Thread Tools

imports after function definitions?

 
 
Hallvard B Furuseth
Guest
Posts: n/a
 
      08-28-2003
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
 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      08-29-2003

"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


 
Reply With Quote
 
 
 
 
Hallvard B Furuseth
Guest
Posts: n/a
 
      08-29-2003
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
 
Reply With Quote
 
Robert Dickinson
Guest
Posts: n/a
 
      08-29-2003
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



 
Reply With Quote
 
Hallvard B Furuseth
Guest
Posts: n/a
 
      09-01-2003
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic imports + relative imports in Python 3 zildjohn01 Python 0 02-22-2011 05:24 PM
relative imports with the __import__ function Chris Colbert Python 3 12-10-2009 10:24 AM
is it "legal" to pace the module's doc string after some imports? Stef Mientki Python 1 10-26-2008 01:09 AM
Imports System.Data or Imports System.Data.SqlClient? Albert ASP .Net 4 07-10-2008 09:00 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57