What is the difference between a syntax error and a runtime error?

For instance:

def tofloat(i): return flt(i) def addnums(numlist): total = 0 for i in numlist: total += tofloat(i) return total nums = [1 ,2 ,3] addnums(nums) 

flt should be a float , but I'm confused if this is a syntax error or a runtime error.

+6
source share
3 answers

Actually, this is a runtime error because Python will try to resolve the flt name at runtime (because it is a dynamic language) and it will not find it. When this happens, Python gives an exception, saying that it could not find the character you used flt , and all this happens at runtime.

Syntax errors occur when the interpreter finds something that does not require Python syntax. For example: Python grammar does not recognize the input syntax as a valid Python program. This can happen if:

  • You forgot to add : at the end of the expression if, def, class , etc.
  • You forgot to close the brackets or brackets, etc.
  • A lot of places when you don't stick with python grammar :)

There is nothing wrong with grammar in your example. For the interpreter, flt(i) is a very valid call to the flt method, which must be checked at runtime within areas if it really exists. This way the interpreter will not complain, and the syntax of your problem is good.

In fact, this can be seen as a drawback of compiled languages ​​such as C #, C ++, etc. Such errors can be detected earlier during compilation, and the compiler screams loudly when it finds it so that you can notice it.

With dynamic languages, you will not notice this until the actual method is called. Your program is simple, so you can quickly find it. But what about the missing o in the float was inside some obsolete structure in a subclass of the subclass of the class, like properties inside some other module, etc. That would be rude :)

UPDATE: The execution model in Python docs is a great read if you understand how Python's internal elements work. This will once again clarify your doubts and give you a lot of knowledge :)

Hope this helps!

+7
source

A SyntaxError is raised by the parser when it detects that your syntax is incorrect, for example, missing colons, brackets, incorrect statements, etc. 'I will not allow you to execute your code until you fix this problem.

Your code will only cause a run-time error, that is, when the tofloat(i) function is called for the first time, so this is a run-time error. In particular, NameError .

Also, a runtime error will not stop the execution of your program until this error is executed. This way your code may work fine if you don't call tofloat ever.

The code below executes correctly to the third line, but then stops when raising a NameError ( NameError error)

 print 1 print 2 print 3 print foo 

output:

 1 2 3 Traceback (most recent call last): File "so.py", line 4, in <module> print foo NameError: name 'foo' is not defined 

This code will not be executed since we made a SyntaxError , although the first 3 lines are fine:

 print 1 print 2 print 2 print (foo 

Output:

 $ python so.py File "so.py", line 5 ^ SyntaxError: invalid syntax 

Please note that there is also a python RunTimeError that occurs when an error is detected that does not fall into any of the other categories

+2
source

You have a NameError , your code should read:

 def tofloat(i): return float(i) 

There is no flt method in flt , so it does not work for you.

By the way, you really don't need to wrap this floating casting into a function, and all of your code can be written as:

 def addnums(numlist): return sum(map(float, numlist)) 

Using it:

 >>> addnums(range(4)) 6.0 
0
source

Source: https://habr.com/ru/post/947943/


All Articles