-1

I'm beginning to learn Python by following an introductory text and have come across an exercise that implements a for loop to read files from a directory. I've used for loops before in R and Java and I have some questions. I'm hoping the community could help me wrap my head around about for loops in general.

  1. It seems (in Python at least) that special characters and numbers can't be used as the "name" in loop. What's the reason for this?

    ex:

    for @ in data:
        print (@, end= ' ')
    

    will produce an error. However replacing @ with x or name for example is fine. Is this one of those things we accept, don't question, and move on?

  2. How does the for loop know (or evaluate) whether an object is "iterable"?

martineau
  • 119,623
  • 25
  • 170
  • 301
the_witch_dr
  • 123
  • 1
  • 9
  • 6
    1/ Because such are the rules for Python variable names. See https://docs.python.org/3/reference/lexical_analysis.html#identifiers – 9769953 Sep 06 '19 at 13:54
  • As stated in the doc, '... the valid characters for identifiers are the same as in Python 2.x: the uppercase and lowercase letters A through Z, the underscore _ and, except for the first character, the digits 0 through 9.' – Thierry Lathuille Sep 06 '19 at 13:55
  • 4
    2/ It doesn't. If the Python runtime finds out an object is not iterable, you'll get an error, something along the lines of `TypeError: 'int' object is not iterable`. – 9769953 Sep 06 '19 at 13:55
  • 1
    "Is this one of those things we accept, don't question, and move on": you can certainly question it, but there is the necessary logic and though behind it. Mostly not to confuse programmers, and to not clash with other tokens (e.g., a `#` as a variable name would make it impossible to have comment lines. `@` at the start of a line also has a special meaning). – 9769953 Sep 06 '19 at 13:58
  • For a (much) more detailed explanation, you can have a look at https://stackoverflow.com/questions/9884132/what-exactly-are-iterator-iterable-and-iteration – Thierry Lathuille Sep 06 '19 at 13:58
  • @ThierryLathuille thank you I will look at the link provided – the_witch_dr Sep 06 '19 at 14:00
  • 1
    If you could use `5` as a variable name, how would you write the number five? – jasonharper Sep 06 '19 at 14:00
  • @jasonharper good point – the_witch_dr Sep 06 '19 at 14:01

1 Answers1

1
  1. As others have mentioned, see the rules for Python variable, i.e. identifier, names: https://docs.python.org/3/reference/lexical_analysis.html#identifiers

  2. An object is iterable if it has the __next__() method defined. See also https://wiki.python.org/moin/ForLoop

Daniel Goldfarb
  • 6,937
  • 5
  • 29
  • 61
  • 1
    Note: object is iterable if it has `__iter__` defined. It is just very often that `__iter__` returns `self` so then `__next__` is used afterward. – Yevhen Kuzmovych Sep 06 '19 at 14:11
  • @YevhenKuzmovych Reading https://docs.python.org/3/reference/datamodel.html#object.__iter__ , I'd say `__iter__` returns an iterator. But the iterator itself requires `__next__`. To me, this sounds like food for confusion whether an object is iterable (adjective), or an iterable (noun) itself. – 9769953 Sep 06 '19 at 14:48
  • @00 Fair point. But considering the second question from the post: `for` will iterate through object only if it has `__iter__` defined. To be precise: `for` will call `__iter__` on the object and iterate through whatever `__iter__` returns. – Yevhen Kuzmovych Sep 06 '19 at 14:52