I have run into this several times. I'm dealing with a lot of methods that can accept a list of strings. Several times I have accidentally passed a single string and it gets broken apart into a list and each character is used, which isn't the desired behavior.
def test(a,b):
x = []
x.extend(a)
x.extend(b)
return x
x = [1,2,3,4]
What I don't want to happen:
test(x,'test')
[1, 2, 3, 4, 't', 'e', 's', 't']
I have to resort to a strange syntax:
test(x,['list'])
I would like these to work implicitly:
test(x,'list')
[1, 2, 3, 4, 'test']
test(x,['one', 'two', 'three'])
[1, 2, 3, 4, 'one', 'two', 'three']
I really feel like there's a "pythonic" way to do this or something involving duck typing, but I don't see it. I know I could use isinstance() to check if it's a string, but I feel like there's a better way.
Edit: I'm using python 2.4.3