3

Basic for loop, I need help understanding how this loop words:

word = "hello"
for word in word:
  print word

Wouldn't the word=hello variable be overwritten with word=h as soon as the for loop started? If so, how does it still loop through all the letters in the word string?

Thanks in advance for the clarification.

Djfluffwug
  • 33
  • 3

2 Answers2

12

Let's look at the bytecode:

>>> def so25807731():
...   word = "hello"
...   for word in word:
...     print word
... 
>>> import dis
>>> dis.dis(so25807731)
  2           0 LOAD_CONST               1 ('hello')
              3 STORE_FAST               0 (word)

  3           6 SETUP_LOOP              19 (to 28)
              9 LOAD_FAST                0 (word)
             12 GET_ITER            
        >>   13 FOR_ITER                11 (to 27)
             16 STORE_FAST               0 (word)

  4          19 LOAD_FAST                0 (word)
             22 PRINT_ITEM          
             23 PRINT_NEWLINE       
             24 JUMP_ABSOLUTE           13
        >>   27 POP_BLOCK           
        >>   28 LOAD_CONST               0 (None)
             31 RETURN_VALUE        

Notice how first, Python grabs an iterator for the string (GET_ITER) and loops through that, rather than the actual string (FOR_ITER).

Therefore, it doesn't need the original string to "remember" what the characters were; it simply uses the newly created iterator for that. The "old word" value is effectively no longer used, so you can overwrite it without problems. Similar logic explains why this code can work as well:

word = "llamas"
for character in word:
  word = None
  print character
tckmn
  • 57,719
  • 27
  • 114
  • 156
  • http://www.shutupandship.com/2012/01/understanding-python-iterables-and.html Here is also good explenation how it works :) – gkocjan Sep 12 '14 at 12:01
  • Worth pointing out that because a `for` loop doesn't create a new scope, the name `word` no longer references the string `"hello"`, but the string `"o"`, after the loop completes. – chepner Sep 12 '14 at 12:07
  • Perfect, I had a slight suspicion this is how it worked but this really helped me understand it! Thank you so much :) – Djfluffwug Sep 12 '14 at 12:14
  • @Djfluffwug Glad to help! `dis` is quite a useful module when you want to know what's really happening "behind the scenes." – tckmn Sep 12 '14 at 12:22
  • 1
    That said, *please don't do this*. If `word` is an appropriate name for your sequence, then it cannot realistically also be an appropriate name for your iteration variable. – Karl Knechtel Sep 12 '14 at 12:57
-3

i never coded python, but i gues it would work like this

word = "hello"
i=0
while i <len(word):
     print word[i]
     i += 1
john Smith
  • 17,409
  • 11
  • 76
  • 117
  • One can see that you never coded Python. This is a bunch of anti-patterns. – Matthias Sep 12 '14 at 12:01
  • 1
    but it don't answer question. Question was why it print whole word, and not only first letter. – gkocjan Sep 12 '14 at 12:03
  • This is actually a bit different, since in this code `word` still references the string `"hello"` after the loop completes. – chepner Sep 12 '14 at 12:10