+9

Python is behaving like a compiled language.

Consider: if True: pass else: do nothing # SyntaxError : invalid syntax Since Python is an interpreted language, the interpreter will take 1 line at a time and execute it. When it finds that the condition in if statement is true, it won't go to else block and program should finish here. But it doesn't. It is only possible in compiled languages where the compiler reads whole code at a time. So, why is Python behaving like that?

1/11/2019 9:46:33 AM

H™L

15 Answers

New Answer

+13

Well, Python (CPython implementation to be precise) its an interpreted AND a compiled programming language because the python src codes will be translate in a bytecode that will be executed by Python Virtual Machine (similar to Java). The problem with your sample is than python source "compilation" involved multiple steps. One of is parsing source file and transform them in an tree of symbols BUT following some rules (eg. syntax rules) and if an of those rules is breaked, python stop "compilation" step emitting an error (in your case "do nothing" do it becuse dont follow syntax python rules). In other sample, no error is raised while "gibberish" is encountered at runtime because name references are checked at runtime and not at pseudo-compile time

+13

Actually there are several ways to "prove" that python code is (at least in some situations) not executed line by line. The most obvious example is a function which isn't executed before it is called. Nevertheless, the syntax within the function is still checked when the module is loaded and the same thing happens to your if/else statement. As already pointed out, the else statement isn't executed, but checked for correctness. Here's a much more interesting example that shows that even formally correct code (that doesn't raise a SyntaxError) isn't necessarily executed line by line: my_var = 5 def my_func(): print(my_var) my_func() # output: 5 Here, we use the variable my_var within the function my_func(). We don't define a local variable with the same name within my_func(), so python assumes that we're referring to the global variable. However, if we change it like this: my_var = 5 def my_func(): print(my_var) # more code # more code # more code # more code # more code my_var = 42 my_func() # error! , we won't be able to print(my_var) from within my_func() because python already "knows" that we're going to reassign my_var later (!) in the function body (what makes it a local variable to my_func()). When we try to call the function, we get an UnboundLocalError: local variable 'my_var' referenced before assignment.

+9

@'gibberish': The gibberish thing won't throw a SyntaxError because it isn't syntactically (I hope that is a word) wrong. Python can't know in advance that some variable won't be defined at a certain point of your code before actually executing the code. "print(gibberish)" is a valid statement and that's all that matters at that point. While "do something" is under no circumstances correct. You could declare a 'gibberish' variable like this: x = 'big', 'rich' a = x[0][::-1] b = a[-1] + chr(0x65) + x[1].replace('c', 's') exec(f'{a}{b} = "{b}{a}"') print(gibberish) # output: berishgib There's no way for python to "understand" what this code does and that it declares a 'gibberish' variable before actually running it. By implication, just because there is no explicit "gibberish = (...)" declaration anywhere in the code, python cannot assume that 'gibberish' will be undefined at runtime.

+7

Before the script runs, the syntax is checked. So if you write code that can never work, you get an error. 'do nothing' is two names it a row, and it seems Python doesn't accept that as proper syntax. You can also try to write 'a b' in your interpreter - you get a SyntaxError. print(gibberish) is no mistake, it is correct syntax, so it passes. But if that part of code is ever executed and the name is not defined at that moment, THEN you get a runtime error.

+5

[important keyword / concept: lexing] https://en.m.wikipedia.org/wiki/Lexical_analysis (You have to pass the lexer before you can get to in-program interpreter behavior) *** edit *** https://docs.python.org/3/reference/lexical_analysis.html "This chapter describes how the lexical analyzer breaks a file into tokens [for the Python parser]."

+3

I hope you didn't type 'do nothing' because it's not a statement. I did this: a=3 if True: pass else: pass print(a) # 3

+3

It's an interpreter, but it still scans the input file(your program) for syntax errors and notifies you of them. (Trust me, I've had hundreds of times I forgot a colon(:), lol!!) The program won't run.

+2

Interesting observation. Unfortunately, it did the same thing for me and I don't have an answer. I do know that if you change the if statement to: if False: you will definitely get an error for an undefined variable

+2

No, this way: if False: print(1) else: print(gibberish) Undefined variable.

+2

Thanks Anna for your answer.

+2

very good!

+1

Jay Matthews I did "do nothing". And that's what i am asking. If the condition in if statement is true, than the statements in else block do not matter. So, it shouldn't have produced syntax error.

+1

Jay Matthews What about this then if True: print(1) else: print(gibberish) # output 1 Here "gibberish" is undefined name, but the interpreter does not give error like " name 'gibberish' is not defined"

+1

Jay Matthews Nope. if False: print(gibberish) Still gives no error.

0

Hey Anna put your example into Code. Btw. do you have a better name for the code? https://code.sololearn.com/csvF44Ljo1Ql/?ref=app