473,396 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

imports after function definitions?

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
4 1417

"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Putz Ronald | last post by:
Hy! I am trying to convert a win Oracle application to solaris! We have use external c functions which I have ibncluded in a shared object. See the makefile below: OBJECTS = definitions.o...
0
by: kyancy | last post by:
Hello All. We have several XML schemas to describe common component document parts. We then create new XML schemas as necessary that use "xsd:import schemaLocation=whateverLocation.." to include...
5
by: Kobu | last post by:
Does anyone know how old style function definitions differ in "behaviour" to new style function definitions? I heard the old style function definitions caused integeral promotion and floating...
19
by: Deniz Bahar | last post by:
Hi, I would like to call one of my functions the exact name as an existing C library function (for example K&R2 exercises asks me to make an atof function). If I don't include the header with...
10
by: Xiaoshen Li | last post by:
Dear All, I am confused with prototypes in C. I saw the following code in a C book: void init_array_1(int data) { /* some code here */ }
8
by: Olov Johansson | last post by:
I just found out that JavaScript 1.5 (I tested this with Firefox 1.0.7 and Konqueror 3.5) has support not only for standard function definitions, function expressions (lambdas) and Function...
20
by: svata | last post by:
Hello there, after some time of pondering I come to some solution which would suit me best. Please correct, if I am wrong. Function has two parameters. A string array, better said a pointer to...
5
by: kimiraikkonen | last post by:
Hello, I want to ask about "imports" statement. Some projects must be inserted with "imports xxxx" statements before beginning coding. But how do i know when to use or do i have to use "imports"...
4
by: Albert | last post by:
Hi, when do i need this: Imports System.Data and when this: Imports System.Data.SqlClient? More specifically, when i want to perform a select or update of a table using: connection = New...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.