0

I have a weird Python beginner's question,.. playing around in my virtual env in the interpreter (python 3.5):

I have a list with mixed types:

lissi = ["foo", "bar". "boo", "baz", 4, 7]

And then "accidentally" try to print out all elements in a for loop concatenated to a string:

for x in lissi:
   print("Hallo " + x)

This, of course, is not possible bcs. we cannot conc. integers to a String - so the first elements are printed and then there is a TypeError.

Then I typed just "x" and enter to see if there is still data stored and yes it is: x is 4.

type(x) is int (tried that to figure out if 7 is also still there).

So my question is: What happens "under the hood" in Python in the for loop: Seems as if every successfully processed element is removed, but there is a backlog stored in x which is the first element the TypeError was thrown for? And is there a way to "clear" this data from memory in case of errors?

thx

3 Answers 3

2

The for loop is part of the scope it is declared in, so the following code will change the value of x:

x = 9

for x in xrange(3): # iterate 0, 1, 2
    print(x)

print(x) # x == 2, x != 9

When the element was "baz", everything was okay and the print statement worked. Python then continued execution. The next step was x = 4. After that, print "Hallo" + x failed with an error.

While running the interpreter, errors are caught and printed, then execution continues. There is no cleanup after an error in the interpreter, so the last value of x is still there when you check the value. This is why you see 4.

Sign up to request clarification or add additional context in comments.

Comments

1

It's not a backlog and nothing like "every successfully processed element is removed".
Basically on every iteration for loop assigns to variable x the value of next element of list lissi (doesn't have to be a list, can be any iterable).
Whenever the loop breaks, due to exception or break statement, variable x is left intact, so it contains the last value assigned by the for loop.
This is ok, since loops don't have any isolated scope in Python and actually makes it convenient to search iterable for interesting element, breaking the loop when it's found and not being forced to store it in another variable before leaving the loop.

1 Comment

Thanks for the "this is ok, since loops[...]" part, so I do understand I do not have worry that in the very unlikely case s.o. would loop through a mixed set of thousands of elements without checking and knowing their types there would be a memory/scope issue in the end :-)
-1

I believe the problem is not with the for loop, but with the print() statement.

You cannot + a string with an integer, example:

This will not work:

print("Hallo " + 4)

nor will this:

print(4 + " Hallo")

if it is all integers it will work:

print(4 + 1)

the error which is shown from print("Hallo " + 4) is "builtins.TypeError: Can't convert 'int' object to str implicitly"

The solution is to do the conversion from integer to string explicitely:

for x in lissi:
   print("Hallo " + str(x))

Now, for your question, I do not believe there is something as a "back log". The enumeration of for x in lissi is still valid, and x is still valid, just that while processing the first 4 enumerations (where x is a string) the print() statement will work, then it throws an error on the print() statement, but x is still a valid integer.

8 Comments

OP knows the reason of TypeError but wants to understand how for loop works.
edited answer, x is valid in the for loop, at all times.
@EdwinvanMierlo That's not the point either. OP wants to know why the value of x from the for loop is remembered after an exception.
@Aaron3468, I did not get that from the question. I see now that the OP didn't know that a break of the loop will retain variable values/types after the break.
@EdwinvanMierlo No worries, sometimes answering is a difficult task. With the language barrier I had trouble figuring out what was meant until I tried what he seemed to be doing. Given the quality of your answer, I don't think you'll have trouble answering future questions :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.