In Python, how would I decide whether an object is iterable?

Posted on
Wed Jul 20, 2022 5:12 am
Lakshit offline

In Python, how would I decide whether an object is iterable?

Is there a technique like iterable? The main arrangement I have found so far is to call

Code: Select all
hasattr(myObj, '__iter__')

In any case, I don't know how secure this is.

Posted on
Wed Jul 20, 2022 6:09 am
FlyingDiver offline
User avatar
Posts: 7217
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: In Python, how would I decide whether an object is itera

I don't see any reason that wouldn't be acceptable. How are you getting these objects in the first place?

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Wed Jul 20, 2022 10:47 am
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: In Python, how would I decide whether an object is itera

That might not catch all possible iterables. The best idea is to just try to iterate and catch any exceptions (it'll be a TypeError if it's not iterable) and do whatever is appropriate.

[MODERATOR NOTE]: moved to a more appropriate forum.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 10 guests