Suggestions for good programming practices?
David LeBlanc
whisper at oz.net
Mon Jun 24 18:29:49 EDT 2002
<snip>
>
> * Avoid exec, execfile, eval, and input.
Might one ask why? What do you have to know to use them successfully? Does
"input" imply that "input-raw" is also to be avoided?
Avoid "from foo import *"; this avoids unnecessary namespace pollution and
other forms of confusion.
> * Use local variables whenever possible. Important in Python because
> local variables are much faster than global. In practice, this
> means that people will often put their main code in a function, and
> call it with the "if __name__=='__main__': main()" cliche.
Does code run at global scope pay a global scope lookup cost?
And for large loops in defs that use class instance data, copying the data
to a local variable to avoid the lookup cost each time through the loop.
(Does copying globals to local also give a speed gain?)
class myclass():
def __init__(self):
self.min = 1
self.max = 1000
def loop(self):
min = self.min
max = self.max
for i in range(min,max):
print i
> * Learn how references and the object system work as soon as possible.
> You should not be surprised when you do this:
>
> a = [1,2,3]
> b = a
> b[0] = 100
> print a
>
> And it prints "[100,2,3]"
The one that always gets me is "lat,lon,timezone = getLocation("seattle")".
I.E. the ability to return multiple distinct values as opposed to returning
multiple values in the form of a struct as in C or C++.
I would add to the list:
* Learn to think in OO and pattern concepts.
> --
> CARL BANKS http://www.aerojockey.com
> "Nullum mihi placet tamquam provocatio magna. Hoc ex eis non est."
Dave LeBlanc
Seattle, WA USA
" Curses! self.bdfl will never work! Back to the drawing board Pinky!"
More information about the Python-list
mailing list