Test for structure

Steven Bethard steven.bethard at gmail.com
Wed Feb 16 14:39:30 EST 2005


alex wrote:
> So how can I test if a variable 'a' is either a single character string
> or a list?

py> def test(x):
...     return (isinstance(x, list) or
...             isinstance(x, basestring) and len(x) == 1)
...
py> test('a')
True
py> test('ab')
False
py> test([])
True
py> test(['a', 'b'])
True

But definitely read Simon Brunning's post - you probably don't actually 
want to do this test.  Why do you think you want to test this?  What's 
your use case?

STeVe



More information about the Python-list mailing list