0

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".
AER
  • 1,549
  • 19
  • 37
  • 1
    I'm not sure exactly what you are asking. Can you rephrase...? Thanks. – CppLearner Feb 21 '14 at 01:07
  • 4
    Why *would* they be defined that way? Non-iterables are not iterable; why would you want the interpreter to magically pretend that they are? – Henry Keiter Feb 21 '14 at 01:07
  • Umm, because 2 isn't in 2? – wim Feb 21 '14 at 01:09
  • Didn’t you just answer yourself? “promotes sloppiness” – Robert Siemer Feb 21 '14 at 01:09
  • Okay. Hengry cleared up the fog. I see. He's asking iterating ``[2]`` vs 2. – CppLearner Feb 21 '14 at 01:09
  • int objects are not iterable – PSeUdocode Feb 21 '14 at 01:09
  • I get the point that you can't do it. I'm not a computer scientist. More a mathematician. This maybe something more to do with variable types and the `__iter__` function. – AER Feb 21 '14 at 01:22
  • 1
    If you are coming from mathematics, think of sequence. There is little distinction between applied mathematics and computer science. – CppLearner Feb 21 '14 at 02:05
  • 1
    As a mathematician, consider that the Axiom of Foundation implies that no set is an element of itself. You're asking to treat `2` as a collection that can be iterated, and find `2` as the only member. This is not intuitive either to mathematicians or to programmers :-) – Steve Jessop Feb 21 '14 at 02:28
  • 2
    Voting to reopen. This is a legitimate language design question. It is certainly possible to let non-iterables iterate as a single element collection. It would be worthwhile conversation to tease out the ways that would make the language better and how it would make it worse. – Raymond Hettinger May 17 '14 at 17:09

4 Answers4

3

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.

Community
  • 1
  • 1
Serdalis
  • 10,296
  • 2
  • 38
  • 58
  • Would making it an iterable create undue resource drain for something as common and as small as an int? Or is it that some programmers would not want to be able to iterate an int in a bug and to have an exception thrown? P.S. If there is a better place for this more discussion type format than a question I should probably move this here if need be. – AER Feb 21 '14 at 01:27
  • It *wouldn't make any sense*, plain and simple. – Karl Knechtel Feb 21 '14 at 01:39
  • To add a comment after learning more coding. It's to do with typed systems. As a noob I didn't realise this, and I wanted to create a for loop which could basically accept anything. Bad design really. – AER Nov 07 '19 at 06:01
  • @AER Nothing wrong with wanting to try everything! Trying to do things that may or may not be possible is a great way to learn. – Serdalis Nov 07 '19 at 06:05
1

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)?

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • Should I edit the comment to be a bit clearer about single object. Probably related to the "It also clashes with strings being iterable by character" part. – AER Feb 21 '14 at 01:17
1

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
tdelaney
  • 73,364
  • 6
  • 83
  • 116
0

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.