I am trying to solve the common "print a character after every item in a list, except the last one" problem.
I do not want to use list indexes. There are a couple of simple ways to do this in Python, but I am not sure how to apply them to my code.
Is there a more readable/couple liner way to do this?
def echo(*args): for data in args: if data is args[-1]: ender = '' else: ender = ' ' if isinstance(data, six.string_types) \ or isinstance(data, six.integer_types): print_(data, end=ender) else: op = getattr(data, '__str__', None) if op: print_(data.__str__(), end=ender) else: LOG.error("echo called with an unsupported data type") print_('')
Note: Code works in both Python 2 and Python 3.