I’m trying to overload my functions by passing a single argument which might be a string or a list of strings. Whats the simplest way to end up with a list of strings?
>>> foo
'bar'
>>> bar
['foo', 'bar']
def myfunc(arg):
listarg = list()
listarg.extend(arg)
print listarg
>>> myfunc(bar)
['foo', 'bar']
>>> myfunc(foo)
['b', 'a', 'r']
When I pass foo I want to see ['bar'].
I’ve fooled around with using isinstance(arg, str) or isinstance(arg, unicode) and isinstance(arg, collections.Iterable) but am not thrilled about them since the latter doesn’t work and the former, well can’t I just join to a list or something – I though str would do it but then unicode came along and am now afraid there might be more of them to test against.
In Python 2,
strandunicodeboth derive frombasestring.Alas, in Python 3, there is no such common parent class. Here you shoud test for
strandbytes.