[Tutor] recursive problem
Joel Goldstick
joel.goldstick at gmail.com
Thu Sep 9 18:41:40 CEST 2010
On Thu, Sep 9, 2010 at 12:07 PM, Steven D'Aprano <steve at pearwood.info>wrote:
> 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>
>
>
> I was googling, and found that if an object has an __iter__ method it will
> return it. If not it will throw an error. You are right about more than
> lists and tuples being iterable. But, in this thread, it was brought up
> that checking type may not be pythonic. If you wanted to use the same code
> to find values in a nested list of any objects, you could dispense with the
> type checking and just see if it is iterable.
>
I'm new to python. Am I off base?
>
>
> --
> Steven D'Aprano
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
--
Joel Goldstick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100909/ad3a22b7/attachment.html>
More information about the Tutor
mailing list