Basic question about speed/coding style/memory

88888 Dihedral dihedral88888 at googlemail.com
Mon Jul 23 17:42:53 EDT 2012


Jan Riechers於 2012年7月21日星期六UTC+8下午3時33分27秒寫道:
> Hello Pythonlist,
> 
> I have one very basic question about speed,memory friendly coding, and 
> coding style of the following easy "if"-statement in Python 2.7, but Im 
> sure its also the same in Python 3.x
> 
> Block
> #----------------------------------
> if statemente_true:

if an evaluated expression result is non-zero, then 


> 	doSomething()

> else: 
# execute this block if  the expression evaluated as zero
 
> 	doSomethingElseInstead()
> 
> #----------------------------------
> 
> versus this block:
> #----------------------------------
> if statement_true:
> 	doSomething()
> 	return
> 
> doSomethingElseInstead()
> 
> #----------------------------------
> 
> 
> I understand the first pattern that I tell the interpreter to do:
> Check if the conditional is true, run "doSomething()" else go inside the 
> else block and "doSomethingElseInstead()".
> 
> while the 2nd does only checks:
> doSomething() if statement_true, if not, just go directly to 
> "doSomethingElseInstead()
> 
> 
> Now, very briefly, what is the better way to proceed in terms of 
> execution speed, readability, coding style?
> Letting out the fact that, in order to prevent 
> "doSomethingElseInstead"-Block to execute, a return has to provided.
> 
> Thank you for reading and hope someone brings light into that.
> 
> Your fellow python programmer
> Jan

Well, the C-style branching is inherited in python.

Expressions and statements are different.



More information about the Python-list mailing list