Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When there are two different errors in a statement, how does Python decide which one to display?

Tags:

python-3.x

When we type in the interpreter the following:

10(b)

We get a NameError that b is not defined. However, when we try:

10(4)

Now, we get the error message that int is not callable. Why didn't Python give the same error in the first case above, since even there, int was not callable? In other words, in the first snippet, we have both errors of "int not callable" and "NameError". If Python were to scan left to right, it would have first seen the call operation being performed on an int (10). So it should have given the "int not callable" error for the first one too. Why didn't it?

I assumed that this was probably because it evaluates the expression within parenthesis first. However, when we try this:

f(print("Hello"))

Now, it says NameError that f is not defined. It doesn't even print "Hello". So this indicates that our assumption of evaluating parenthesized expressions doesn't seem to hold.

So how exactly does it work?

like image 582
Programmer Avatar asked Jan 12 '19 20:01

Programmer


People also ask

How does Python handle multiple errors?

By handling multiple exceptions, a program can respond to different exceptions without terminating it. In Python, try-except blocks can be used to catch and respond to one or multiple exceptions. In cases where a process raises more than one possible exception, they can all be handled using a single except clause.

How does Python handle specific errors?

In Python, exceptions can be handled using a try statement. The critical operation which can raise an exception is placed inside the try clause. The code that handles the exceptions is written in the except clause. We can thus choose what operations to perform once we have caught the exception.

What the the two main types of errors in Python?

There are mainly two types of errors in python programming namely - Syntax errors and Logical Errors or Exceptions. Whenever we do not write the proper syntax of the python programming language (or any other language) then the python interpreter throws an error known as syntax error.

How do you check multiple conditions in one if statement?

Here we'll study how can we check multiple conditions in a single if statement. This can be done by using 'and' or 'or' or BOTH in a single statement. and comparison = for this to work normally both conditions provided with should be true. If the first condition falls false, the compiler doesn't check the second one.


1 Answers

You are simply executing valid expressions, which have runtime errors. The code is executed in the stated evaluation order, with operators executed according to their precedence. Within each expression, you need to check the documentation for that specific operation to see what order the parts of the operation are executed in.

In all three cases you are asking Python to execute a call, for which the rules are documented:

The primary must evaluate to a callable object[.] All argument expressions are evaluated before the call is attempted.

The callable is evaluated to an object first, and 10 successfully executes to produce an object. No call is attempted yet until the argument expresssions have been evaluated.

The argument expressions are the expressions that make up the values to be passed to the call, the bits inside the (...). Only once those expressions have been evaluated, the call is is made.

Your first example gives a NameError exception, because the b expression evaluation failed. Python never got to executing the call because evaluation of the argument expressions has failed.

In your second example, the argument expression is 4, a valid expression that succeeds, so only then can Python can proceed to executing the call. But 10 is not callable, so that is then the next failure.

The final example fails right at the start at the f expression, because Python evaluated the callable part first but can’t load the callable because the name is not defined.

For other operators different rules of evaluation order apply, you have to check to documentation for that specific operator to see in what order the component parts will be executed.

like image 88
Martijn Pieters Avatar answered Nov 10 '22 01:11

Martijn Pieters