Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #64521
| From | Dave Angel <davea@davea.name> |
|---|---|
| Subject | Re:No overflow in variables? |
| Date | 2014-01-22 13:26 -0500 |
| Organization | news.gmane.org |
| References | <dd6a72bf-b1c4-4868-9cfe-76dfac8b8787@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.5845.1390415074.18130.python-list@python.org> (permalink) |
Philip Red <filippo.biolcati@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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
No overflow in variables? Philip Red <filippo.biolcati@googlemail.com> - 2014-01-22 10:09 -0800
Re: No overflow in variables? Larry Martell <larry.martell@gmail.com> - 2014-01-22 11:18 -0700
Re:No overflow in variables? Dave Angel <davea@davea.name> - 2014-01-22 13:26 -0500
Re: No overflow in variables? Philip Red <filippo.biolcati@googlemail.com> - 2014-01-22 10:32 -0800
Re: No overflow in variables? Chris Angelico <rosuav@gmail.com> - 2014-01-23 05:26 +1100
Re: No overflow in variables? Roy Smith <roy@panix.com> - 2014-01-22 15:55 -0500
Re: No overflow in variables? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-01-23 11:22 +1300
Re: No overflow in variables? Philip Red <filippo.biolcati@googlemail.com> - 2014-01-22 10:48 -0800
Re: No overflow in variables? random832@fastmail.us - 2014-01-22 18:13 -0500
Re: No overflow in variables? Christian Heimes <christian@python.org> - 2014-01-23 10:14 +0100
Re: No overflow in variables? Chris Angelico <rosuav@gmail.com> - 2014-01-23 20:36 +1100
csiph-web