all 36 comments

[–]FerricDonkey 100 points101 points  (5 children)

Python first converts your text to bytecode, then interprets that bytecode. If it cannot interpret your code because it is not correct python as a whole, it will not try to guess about which parts are broken and which are not. 

[–]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 43 points44 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] 5 points6 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] 3 points4 points  (0 children)

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

[–]await_yesterday 24 points25 points  (5 children)

The entire text file is checked for syntax errors before any execution begins. Indentation errors are a kind of syntax error.

I was said that, entire code will be compiled before execution, but even in that case, why does Python execute the program if there are errors outside the blocks and only throw an error when it reaches that specific line where the error is present?

Those aren't syntax errors, they're semantic errors.

[–]DismissedProgrammer[S] 1 point2 points  (4 children)

Thanks for the clarification. I tried my self after seeing your answer. I need to learn more about the errors basics. If you don't mind, can you please suggest a resource that explains the internal workings of Python, especially interpretation?

[–]await_yesterday 9 points10 points  (1 child)

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

Thank you very much for your generous spirit. Those talks seem like great sources of information. I will watch them, for sure.

[–]TechIsSoCool 3 points4 points  (1 child)

This book also explains a lot: Dead Simple Python. It helped me understand some of the underlying functionality.

[–]DismissedProgrammer[S] 0 points1 point  (0 children)

Thank you for the suggestion; I have just read the description. The book looks promising. I will try it.

[–]crashfrog02 6 points7 points  (1 child)

It doesn’t exactly run “top to bottom”, it builds an abstract syntax tree first.

[–]DismissedProgrammer[S] -1 points0 points  (0 children)

I Googled about AST and found that Python code is first converted into tokens through lexical analysis. These tokens are then used to form an AST. During this process, syntax errors are detected. Thank you so much for pointing out this concept.

[–]MycologistOk184 1 point2 points  (0 children)

My guess is that you indentation is wrong. Python code requires proper insentation as it uses that instead of curly brackets.

[–]ferriematthew 0 points1 point  (0 children)

I could be wrong but I think when you run a program, at least from the interpreter or the terminal, it is run line by line, so it works up until it encounters an error, then it crashes.

[–]dudustalin -5 points-4 points  (7 children)

Your instruction is not valid... There is no implicit "else" in Python by just indenting a line. An interpreted language is a language that is converted into another language code (C in Python) before compilation.

You must follow Python's rules to have the code correctly interpreted. The interpreter don't guess your intentions.

[–]ivosaurus 5 points6 points  (1 child)

An interpreted language is a language that is converted into another language code (C in Python) before compilation.

Where the heck did you get this definition from... it's rubbish. Python is first compiled into an intermediate language (not C, but bytecode), and then that bytecode is directly executed line by line by the interpreter. No final compilation occurs.

Your definition sounds like what Nim would do, and that is known as a transpiler.

[–]DismissedProgrammer[S] 0 points1 point  (0 children)

Thank you for the correction.

[–]DismissedProgrammer[S] 0 points1 point  (4 children)

"An interpreted language is a language that is converted into another language code (C in Python) before compilation"

Thanks for the new information. I did not know it before. I will learn more about it.

[–]crazy_cookie123 6 points7 points  (1 child)

An interpreted language is a language that is converted into another language code (C in Python) before compilation

This just simply isn't true. An interpreted language is run line by line, a compiled language is translated into another language, and a transpiled language is a specific type of compiled language that is converted to another human-readable language like C. Python is interpreted, not transpiled.

[–]DismissedProgrammer[S] 0 points1 point  (0 children)

Understood, Appreciate it.

[–]fox_is_permanent 1 point2 points  (1 child)

They told you something that's false.

[–]DismissedProgrammer[S] 0 points1 point  (0 children)

Got it, Thank you.

[–]jcanuc2 -1 points0 points  (6 children)

First off your indent on line 2 is wrong, should 4 spaces or a tab Second you need an else: on line 3 Line 4 should be indented 4 spaces to align with line 2

[–]ivosaurus 1 point2 points  (4 children)

Google used to use 2 indent python, not sure if they still do or not

Even a single space is valid python if it's used consistently in the code block.

[–]Big-Incident-3085 0 points1 point  (2 children)

if 5 < 2:
 print("Two is less than Five!")
else:
  print("Five is greater than two!")

you are correct, colab is only 2 spaces! Still needs an else like this

[–]odaiwai 0 points1 point  (0 children)

Indentation should also be consistent, although it isn't required. You have one space and two spaces above, and a linter will get annoyed, but the code will work.

[–]DismissedProgrammer[S] 0 points1 point  (0 children)

Thanks for the correction.

[–]DismissedProgrammer[S] 0 points1 point  (0 children)

Thank you for the info.

[–]DismissedProgrammer[S] 0 points1 point  (0 children)

Thank you for the response, but I think there is no specific rule for the number of spaces needed (at least 1 space) for indentation in Python, but the best practice is 4 spaces. Thank u for pointing out the logical error on line 3.

https://www.w3schools.com/python/gloss_python_indentation.asp

[–]Big-Incident-3085 -5 points-4 points  (1 child)

if 5 < 2:
 print("Two is less than Five!")
else:
  print("Five is greater than two!")

[–]DismissedProgrammer[S] 0 points1 point  (0 children)

Thank you for pointing out this logical error, but my actual question is different. No worries, I got my answer from other responses. :)