[Tutor] How to enable pausing my stop_watch.py ?

Alan Gauld alan.gauld at blueyonder.co.uk
Fri Jul 9 23:19:23 CEST 2004

> >   h_seconds = (hours and 3600*int(hours)) or 0  # shorter but more
> >obscure
> >
> >Works like this:
> >Only evaluate 3600*hours if hours NOT ""
> >If result is False(ie hours was "") then evaluate
> >second part of OR - ie 0
> I'm very glad to know about these uses of "and" and "or", but why is
> way better? Is fewer lines better? Is it faster? More Pythonesque?
> not defending my code; I'd just like to know.)

It's not better, just an alternative way of doing it. Indeed as the
comment says its more obscure and so could be regarded as worse!
However it is also very slightly faster.

If you are a C (or Perl or Java/Javascript) you might be familiar
with the tertiary ?: operator, if so the and/or pair will present
a familiar shortcut and for those programmers the obscurity is
not a problem. I prefer it simply because it is shorter and doesn't
clutter up my main program with if/elses that are tangential to
the main purpose of the program. But tastes differ and I would never
claim the and/or option was better.

Indeed part of the zen of Python is that explicit is better than

Alan G.

More information about the Tutor mailing list