No overflow in variables?

Dave Angel davea at davea.name
Wed Jan 22 13:26:36 EST 2014


 Philip Red <filippo.biolcati at googlemail.com> Wrote in message:
> Hi everyone. First of all sorry if my english is not good.
> I have a question about something in Python I can not explain:
> in every programming language I know (e.g. C#) if you exceed the max-value of a certain type (e.g. a long-integer) you get an overflow. Here is a simple example in C#:
> 
>         static void Main(string[] args)
>         {
>             Int64 x = Int64.MaxValue;
>             Console.WriteLine(x);       // output: 9223372036854775807
>             x = x * 2;
>             Console.WriteLine(x);       // output: -2 (overflow)
>             Console.ReadKey();
>         }
> 
> Now I do the same with Python:
> 
>             x = 9223372036854775807
>             print(type(x))             #   <class 'int'>
>             x = x * 2                  #   18446744073709551614
>             print(x)                   #   <class 'int'>
>             print(type(x))
> 
> and I get the right output without overflow and the type is always a 'int'.
> How does Python manages internally the types and their values? Where are they stored?
> 
> Thank you for your help :)
> 

In python,  every value is an object. Some, like lists, can grow
 over time, and there's no specific upper limit in size. Others, 
 like int, or string,  are immutable,  so the constructor can
 calculate just how much space is needed.

In java, and I believe in C#, they make a distinction between
 unboxed and boxed integers.  The former are NOT objects, and have
 a specific upper bound, generally based on some power of
 2.


-- 
DaveA




More information about the Python-list mailing list