is there any principle when writing python function
usenet-nospam at seebs.net
Tue Aug 23 18:53:40 CEST 2011
On 2011-08-23, smith jack <thinke365 at gmail.com> wrote:
> i have heard that function invocation in python is expensive, but make
> lots of functions are a good design habit in many other languages, so
> is there any principle when writing python function?
Lots of them. None of them have to do with performance.
> for example, how many lines should form a function?
Between zero (which has to be written "pass") and a few hundred. Usually
closer to the lower end of that range. Occasionally outside it.
Which is to say: This is the wrong question.
Let us give you the two laws of software optimization.
Law #1: Don't do it.
If you try to optimize stuff, you will waste a ton of time doing things that,
it turns out, are unimportant.
Law #2: (Experts only.) Don't do it yet.
You don't know enough to "optimize" this yet.
Write something that does what it is supposed to do and which you understand
clearly. See how it looks. If it looks like it is running well enough,
STOP. You are done.
Now, if it is too slow, and you are running it on real data, NOW it is time
to think about why it is slow. And the solution there is not to read abstract
theories about your language, but to profile it -- actually time execution and
find out where the time goes.
I've been writing code, and making it faster, for some longish period of time.
I have not yet ever in any language found cause to worry about function call
Copyright 2011, all wrongs reversed. Peter Seebach / usenet-nospam at seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.
More information about the Python-list