[Tutor] recursive problem

Steven D'Aprano steve at pearwood.info
Thu Sep 9 18:07:11 CEST 2010


On Fri, 10 Sep 2010 01:05:22 am Joel Goldstick wrote:
> On Thu, Sep 9, 2010 at 10:26 AM, Luke Paireepinart
>
> <rabidpoobear at gmail.com>wrote:
> > Shouldn't there be a way to do this without type checking? Duck
> > typing!
> >
> > Your post got me thinking.  Maybe better to test if the object can
> > return
> an iter method.  If it throws an error, then look at its value.  If
> it doesn't, then its a list or a tuple


It's not clear what you mean by "return an iter method". Taken 
literally, that would imply the object is a function. I think you 
mean "*has* an iter method" -- except that's not right either:

>>> [].iter
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'iter'


Perhaps you mean an object which can be passed to iter(), but lots of 
objects can do that, not just lists and tuples:

>>> iter("not a list or tuple")
<str_iterator object at 0xb7d3520c>
>>> iter({1: None, 2: "a", 4: 5})
<dict_keyiterator object at 0xb7d3420c>





-- 
Steven D'Aprano


More information about the Tutor mailing list