In Python, how do I determine if an object is iterable?

Checking for __iter__ works on sequence types, but it would fail on e.g. strings in Python 2. I would like to know the right answer too, until then, here is one possibility (which would work on strings, too): from __future__ import print_function try: some_object_iterator = iter(some_object) except TypeError as te: print(some_object, ‘is not iterable’) The … Read more

Understanding slice notation

It’s pretty simple really: There is also the step value, which can be used with any of the above: The key point to remember is that the :stop value represents the first value that is not in the selected slice. So, the difference between stop and start is the number of elements selected (if step is 1, the default). The other feature is that start or stop may be a negative number, which … Read more

main loop ‘builtin_function_or_method’ object is not iterable

Direct Answer In the code here: change sourceCode.split to sourceCode.split(). If you want to know more about this error, read below: When debugging, you’d better remove the try…except block, especially an “expect Exception” block, which is so generic that you will get lost about what is going wrong. When removed the try…except block and run these code again, … Read more

Understanding slice notation

It’s pretty simple really: There is also the step value, which can be used with any of the above: The key point to remember is that the :stop value represents the first value that is not in the selected slice. So, the difference between stop and start is the number of elements selected (if step is 1, the default). The other feature is that start or stop may be a negative number, which … Read more

Understanding slice notation

It’s pretty simple really: There is also the step value, which can be used with any of the above: The key point to remember is that the :stop value represents the first value that is not in the selected slice. So, the difference between stop and start is the number of elements selected (if step is 1, the default). The other feature is that start or stop may be a negative number, which … Read more