Test for structure

Steven Bethard steven.bethard at gmail.com
Wed Feb 16 16:50:42 EST 2005


Michael Hartl wrote:
> I use a function isListLike in cases such as this one:
> 
> # def isListLike(L):
> #     """Return True if L is list-like, False otherwise."""
> #     try:
> #         L + []
> #         return True
> #     except:
> #         return False
> 
> Then you can use a standard if-else construct:
> 
> # if isListLike(myvar):
> #     <do something>
> # else:
> #     <do something else>

What kind of situations do you use this for?  I almost never have to do 
this kind of typechecking.  If it's supposed to be a list, I just use it 
as a list...

STeVe



More information about the Python-list mailing list