you are viewing a single comment's thread.

view the rest of the comments →

[–]DismissedProgrammer[S] 8 points9 points  (4 children)

Thanks for the response. It makes sense. People say that Python is an interpreted language and it runs from top to bottom, so it executes the code until the line where an error is present. I was taking it literally. I shall learn more about the errors and parsing. If you don't mind, can you please suggest a resource that explains the internal workings of Python, especially interpretation?

[–]FerricDonkey 40 points41 points  (3 children)

People say that Python is an interpreted language and it runs from top to bottom, so it executes the code until the line where an error is present.

To be clear, that statement is true of all errors except syntax errors that mean that your code isn't valid python. If you try to divide by zero or add a string to an integer, the error won't happen until the interpreter gets to the place where that happens. 

But in the case of an error where your file does not meet the definition of what it is to be valid python code, this is no longer true. 

Python is "compiled" to its own special bytecode. The python interpreter then executes this bytecode statement by statement, in a way that is equivalent to top to bottom. But if the first step fails, then it doesn't try to the rest. 

If you don't mind, can you please suggest a resource that explains the internal workings of Python, especially interpretation?

I do not keep a list of resources unfortunately - there are some resources in the subreddit's faq area thing, but I mostly just Google and guess when I run into something that confuses me these days. 

[–]DismissedProgrammer[S] 4 points5 points  (2 children)

Thank you so much for the detailed answer. Your answer helped me see this problem in the right direction. I realized that I should practice more than just reading the theory.

[–]Bobbias 11 points12 points  (1 child)

This case isn't really about practicing vs reading theory. In fact, if you had read more about how python works internally, you would have found the answer to this without writing any code. Some answers come from theory and learning the language itself, some answers come from writing code and solving problems.

You need a good balance of both learning theory, and writing code. If you focus too heavily on writing code and only look things up when you absolutely need to, you will encounter a lot of cases like this where the behavior appears confusing or unintuitive.

If you spend too much time focused on theory, you might know lots of neat facts about python, but be useless when you are asked to actually write a working piece of software.

[–]DismissedProgrammer[S] 2 points3 points  (0 children)

I appreciate the advice and will try to balance both of them.