Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined Behaviour in Python

Tags:

c++

python

c

In C or C++ I know that there is something called

undefined behaviour

In expression evaluation when some of the the expressions have side-effects. let's say I want to calculate the following:

c = 10
f() + g() + c

but at some point g makes c = 5.(c is a glob variable)

What would be the behavior in python? Would it be undefined as C?

like image 852
Mark R. Chandar Avatar asked Mar 01 '18 11:03

Mark R. Chandar


People also ask

Is there undefined behavior in Python?

Python has undefined behavior because it's implementations are written in languages that do.

What is undefined behavior in programming?

In computer programming, undefined behavior (UB) is the result of executing a program whose behavior is prescribed to be unpredictable, in the language specification to which the computer code adheres.

Why does undefined behavior exist?

Undefined behavior exists mainly to give the compiler freedom to optimize. One thing it allows the compiler to do, for example, is to operate under the assumption that certain things can't happen (without having to first prove that they can't happen, which would often be very difficult or impossible).

What type of behavior C is undefined?

So, in C/C++ programming, undefined behavior means when the program fails to compile, or it may execute incorrectly, either crashes or generates incorrect results, or when it may fortuitously do exactly what the programmer intended.

Does JavaScript have undefined behavior?

random() and Date. now() . Is there a piece of JavaScript code for which the behaviour is not completely determined by the JavaScript specifications, and, as such, has "undefined behaviour"? Yes, read the quirksmode.


3 Answers

From 6.15 of python documentation

Python evaluates expressions from left to right. Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side.

In the following lines, expressions will be evaluated in the arithmetic order of their suffixes:

expr1, expr2, expr3, expr4
(expr1, expr2, expr3, expr4)
{expr1: expr2, expr3: expr4}
expr1 + expr2 * (expr3 - expr4)   <----- This is of importance to us.
expr1(expr2, expr3, *expr4, **expr5)
expr3, expr4 = expr1, expr2

So here the functions will be called in order from left to right. So any of the changes you will see will be due to the functions called from left to right.

And yes in python function calls are expressions.

like image 187
user2736738 Avatar answered Oct 19 '22 17:10

user2736738


C code such as this:

#include <stdio.h>

int c;

int f (void)
{
  return 1;
}

int g (void)
{
  return ++c;
}

int main()
{
  c = 3;

  printf("%d", f() + g() + c);

  return 0;
}

does not invoke undefined behavior. It does however invoke unspecified behavior. These are different, formal terms: Undefined, unspecified and implementation-defined behavior

Note first that f() + g() + c is grouped as (f() + g()) + c but that tells you nothing about the order in which the terms themselves are actually evaluated.

The result of this code can either be 1 + (3+1) + 3 = 8 or 1 + (3+1) + 4 = 9, depending on if the operand g() is evaluated before or after the operand c. The order of evaluation of operands of the + operator is unspecified, so we can't know which operand that gets evaluated first, nor should we write code that relies on a certain order.

The code will only ever give either of the two mentioned results, it will not do anything completely crazy, like crashing or giving garbage results, which code containing undefined behavior could do. An example of undefined behavior would be c++ + c++.

The difference between the examples is where the side effects take place. See Undefined behavior and sequence points

like image 22
Lundin Avatar answered Oct 19 '22 16:10

Lundin


If c is a global variable, and g() is making the same c to be the value of 5, meaning that this is unspecified behaviour.

Note, Python interpreters evaluate expressions from left to right, meaning that the c would be 5 when it's added to f() + g().

like image 1
Adi219 Avatar answered Oct 19 '22 16:10

Adi219