Test for structure

Michael Hartl mhartl at post.harvard.edu
Wed Feb 16 15:32:37 EST 2005


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>

Michael

--
Michael D. Hartl, Ph.D.
Chief Technology Officer
http://quarksports.com/




More information about the Python-list mailing list