Sunday, 1 September 2013

String or list of strings to a tuple

String or list of strings to a tuple

I've got a class attribute which can be either a string or a list of
strings. I want to convert it into a tuple such that a list converts
normally, but a single string becomes a single-item tuple:
[str, str] --> (str, str)
str --> (str, )
Unfortunately tuple('sting') returns ('s', 't', 'r', 'i', 'n', 'g') which
is not what I expect. Is it possible to do it without type checking?

No comments:

Post a Comment