if or exception

Matteo Dell'Amico della at toglimi.linux.it
Thu Jul 29 10:01:14 EDT 2004


Thomas Lindgaard wrote:

[...]

> |   def startPages(self):
> |     try:
> |       avgPagesPerHost = numPagesFetched / len(hostDict)
> |     except ZeroDivisionError: 
> |       # this will only happen the first time around when no hosts are
> |       # known
> |       avgPagesPerHost = 42

[...]

I'd do it the way you have done, but I wouldn't call it 
'avgPagesPerHost', since it's a bit misleading, but something along the 
lines of 'perHostLimit'.

I'd write the if/else version as:

if numPagesFetched != 0:
     perHostLimit = ...
else:
     perHostLimit = 42

But since here the standard case seems to be the first one, I think a 
try/except solution is both clearer and more efficient.

-- 
Ciao,
Matteo



More information about the Python-list mailing list