[Tutor] How and where to use pass and continue

Sean Perry shaleh at speakeasy.net
Mon Mar 28 04:28:57 CEST 2005


Kevin wrote:
> I am having lot of trouble learning where and when to use pass and
> continue. The two books that I use don't explian these very good. Is
> there a website the explains these is great detail?
> I have also looked at the python tutorial as well.
> 

language idioms are one of the hardest things to learn and only really 
come after having written code and then coming back to it later.

My thoughts on the subject.

continue is a common idiom in I/O routines.

for line in fp.xreadlines():
     line = line.strip()
     if not line: continue

     # process line data here

pass is an odd one. I tend to use it while prototyping to set up the 
bones of control statements to be.

def func(one, two):
     if sometest(one):
         pass # need to actually do something here

     # work with one and two

pass can also be used in multi-catch functions / control statements

if input == 'something':
     handle_something(input)
elif input == 'other':
     pass
else:
     # do stuff here

We want to ignore 'other' for some reason, so just use pass. Also handy 
if a value could be one of N choices but we only handle a few cases. 
This way someone else reading the code does not think "hey wait, they do 
not check for 'foo'!".


More information about the Tutor mailing list