What is the difference between syntax error and ru

2020-06-19 07:07发布

问题:

For example:

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)

The flt is supposed to be float, but I'm confused whether it is a syntax error or a runtime error.

回答1:

Actually, it is a runtime error, because Python will try to resolve the flt name during runtime (because it's a dynamic language), and it won't find it. When this happens, Python yields and exception saying that it couldn't find the symbol you were using flt and all this happens at runtime.

Syntax errors happen when the interpreter find something not compelling with Python's syntax. For example: The Python's grammar doesn't recognize the input syntax as a valid Python program. This may happen when:

  1. You forgot to add : at the end of an if, def, class, etc expression
  2. You forgot to close some parenthesis or brackets, etc.
  3. A lot of places else when you don't adhere to python's grammar :)

In your example, there is nothing wrong with the grammar. For the interpreter, flt(i) is a very valid call to a flt method which had to be check at runtime within the scopes if it really exists. So the interpreter won't complaint and the syntax of your problem is good.

Actually, this can be seen as a disadvantage over compiled languages like C#, C++, etc. This kind of errors can be detected sooner at compile time, and the compiler screams loud when it find it so you can notice it.

With dynamic languages, you won't notice this until the actual method is called. Your program is simple, so you may find it quick. But, what about the missing o in float was inside some legacy framework within a subclass of a subclass of a class, as a property, inside some other module, etc. That would be harsh :)

UPDATE: The execution model in Python's docs are a great read if you're into how does Python internals works. This will clarify your doubt further and will give you a lot of knowledge :)

Hope this helps!



回答2:

SyntaxError is raised by parser when it founds that your syntax is not correct, like missing colons, parenthesis, invalid statements etc. It'll not allow you to execute your code until you don't fix that the issue.

Your code will throw only error at runtime, i.e when the function tofloat(i) is called for the first time, so it is a runtime error. Specifically NameError.

Also a runtime error won't stop your program's execution until that buggy part is not executed. So, your code can actually run fine if you don't call tofloat ever.

The code below executes properly up to third line but then stops as NameError is raised.(a runtime 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 won't execute as we made a SyntaxError, even though the first 3 lines are perfectly okay:

print 1
print 2
print 2
print (foo

Output:

$ python so.py
  File "so.py", line 5

              ^
SyntaxError: invalid syntax

Note that there's also a RunTimeError in python, which is raised when an error is detected that doesn't fall in any of the other categories



回答3:

You have a NameError, Your code should read:

def tofloat(i): 
    return float(i)

There is no flt method in Python, that is why it is not working for you.

Incidentally, you really don't need to wrap that float casting into a function, and your whole code can be written as:

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

Using it:

>>> addnums(range(4))
6.0