Paul McNett a écrit :

def avg(*args):
  return sum(args) / len(args)

There are some dangers (at least two glaring ones)  with this code,
though, which I leave as an exercise for the reader.

try:
  avg("toto", 42)
except TypeError, e:
  print "this is the first one : %s" % e
try:
  avg()
except  ZeroDivisionError, e:
  print "this is the second : %s" % e

As far as I'm concerned, I would not handle the first one in the avg function - just document that avg expects numeric args.

Not quite sure what's the best thing to do in the second case - raise a ValueError if args is empty, or silently return 0.0 - but I'd tend to choose the first solution (Python's Zen, verses 9-11).
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to