Suppose I have a function which can either take an iterable/iterator or a non-iterable as an argument. Iterability is checked with try: iter(arg)
.
Depending whether the input is an iterable or not, the outcome of the method will be different. Not when I want to pass a non-iterable as iterable input, it is easy to do: I’ll just wrap it with a tuple.
What do I do when I want to pass an iterable (a string for example) but want the function to take it as if it’s non-iterable? E.g. make that iter(str)
fails.
Edit – my original intention:
I wanted to generalise the zip
function in that it can zip iterables with non-iterables. The non-iterable would then repeat
itself as often as the other iterables haven’t finished.
The only general solution fo me seems now to be, that I shouldn’t check inside the general_zip
function (because of the string issues); but that instead I’ll have to add the repeat
iterator to the argument before calling zip
. (This actually saves me from inventing the general_zip
function — although I still might because with a non-iterable as an input it would be unambiguous without the extra repeat.)