Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python number wrapping?

Tags:

python

Consider this Python code:

assert(a > 0)
assert(b > 0)
assert(a + b > 0)

Can the third assert ever fail? In C/C++, it can if the sum overflows the maximum integer value. How is this handled in Python?

like image 202
Colin Avatar asked Sep 24 '10 21:09

Colin


1 Answers

Depends on which version of Python you're using.

Prior to 2.2 or so, you could get an OverflowError.

Version 2.2-2.7 promote the sum to a long (arbitrary precision) if it's too large to fit in an int.

3.0+ has only one integer type, which is arbitrary precision.

like image 134
dan04 Avatar answered Oct 12 '22 23:10

dan04