abcd wrote:

> good point.  is there place that documents what methods/attrs I should
> check for on an object?  for example, if its a list that I expect I
> should verify the object that is passed in has a ??? function? etc.

Don't check, try. Catch a possible exception, and continue with another type
assumption. The only thing one often checks is for basestring, as
basestring supports iteration, but more than often isn't supposed to be
iterated over. 

Small example to gather all strings out of a tree of objects (untested):

def foo(arg):
   # string case
   if isinstance(arg, basestring):
      return [arg]
   # dict-like
   try:
      res = []
      for value in arg.itervalues():
          res.extend(foo(value))
      return res
   except AttributeError:
      pass
   # generally iterables
   res = []
   for value in arg:
       res.extend(foo(value))
   return res

Diez
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to