Question

This may seem like an odd question but why doesn't python by default "iterate" through a single object by default.

I feel it would increase the resilience of for loops for low level programming/simple scripts.

At the same time it promotes sloppiness in defining data structures properly though. It also clashes with strings being iterable by character.

E.g.

x = 2
for a in x:
    print(a)

As opposed to:

x = [2]
for a in x:
    print(a)

Are there any reasons?

FURTHER INFO: I am writing a function that takes a column/multiple columns from a database and puts it into a list of lists. It would just be visually "nice" to have a number instead of a single element list without putting type sorting into the function (probably me just being OCD again though)

Pardon the slightly ambiguous question; this is a "why is it so?" not an "how to?". but in my ignorant world, I would prefer integers to be iterable for the case of the above mentioned function. So why would it not be implemented. Is it to do with it being an extra strain on computing adding an __iter__ to the integer object?

Discussion Points

  • Is an __iter__ too much of a drain on machine resources?
  • Do programmers want an exception to be thrown as they expect integers to be non-iterable
  • It brings up the idea of if you can't do it already, why not just let it, since people in the status quo will keep doing what they've been doing unaffected (unless of course the previous point is what they want); and
  • From a set theory perspective I guess strictly a set does not contain itself and it may not be mathematically "pure".
Was it helpful?

Solution

Python cannot iterate over an object that is not 'iterable'.

The 'for' loop actually calls inbuilt functions within the iterable data-type which allow it to extract elements from the iterable.

non-iterable data-types don't have these methods so there is no way to extract elements from them.

This Stack over flow question on 'if an object is iterable' is a great resource.

OTHER TIPS

The problem is with the definition of "single object". Is "foo" a single object (Hint: it is an iterable with three strings)? Is [[1, 2, 3]][0] a single object (It is only one object, with 3 elements)?

The short answer is that there is no generalizable way to do it. However, you can write functions that have knowledge of your problem domain and can do conversions for you. I don't know your specific case, but suppose you want to handle an integer or list of integers transparently. You can create your own iterator:

def col_iter(item):
    if isinstance(item, int):
        yield item
    else:
        for i in item:
            yield i

x = 2
for a in col_iter(x):
    print a

y = [1,2,3,4]
for a in col_iter(y):
    print a

The only thing that i can think of is that python for loops are looking for something to iterate through not just a value. If you think about it what would the value of "a" be? if you want it to be the number 2 then you don't need the for loop in the first place. If you want it to go through 1, 2 or 0, 1, 2 then you want. for a in range(x): not positive if that's the answer you're looking for but it's what i got.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top