[Tutor] Doubts galore.
Steve Willoughby
steve at alchemy.com
Thu Jun 10 19:11:10 CEST 2010
On Thu, Jun 10, 2010 at 09:43:34PM +0530, prasad rao wrote:
> > It would probably be cleaner to use one-line
> > conditional statements (sparingly) where they make
> > sense on their own, but not to mix multi-line and
> > single line styles in the same if-else structure.
>
> I am a newbie.I couldn't understand that comment.
So occasionally it's cleaner and clearer to put the
condition and statement on one line like
if x>0: print x
But often you need more than one line, so you make
a block structure:
if x>0:
do_stuff()
do_more_stuff()
If you have multiple blocks in an if-elif-elif-...-else
structure, don't go back and forth between one-line
and multi-line blocks, be consistent, so instead of
something like
if x>0: do_one_thing()
elif x<0:
do_something_else(-x)
something_entirely_different(x**2)
elif x==0 and y>0:
do_something_else(y)
yet_another_function(y**3)
else: raise ValueError('values for x and y not in allowed range')
you should be consistent and write this as:
if x>0:
do_one_thing()
elif x<0:
do_something_else(-x)
something_entirely_different(x**2)
elif x==0 and y>0:
do_something_else(y)
yet_another_function(y**3)
else:
raise ValueError('values for x and y not in allowed range')
--
Steve Willoughby | Using billion-dollar satellites
steve at alchemy.com | to hunt for Tupperware.
More information about the Tutor
mailing list