[Tutor] input python 3.3

Alan Gauld alan.gauld at btinternet.com
Tue Feb 4 11:31:28 CET 2014


On 04/02/14 09:01, Ian D wrote:

> I used to use 2.7 and the input was pretty when inputting a numeric
> value, it would just get cast to an int.

Just to be picky it would get converted to an int not cast
as an int.

casting and converting are two very different things.
casting means treat a bit pattern in memory as if it is
a particular type regardless of how that bit pattern got there.
Converting to a type changes the bit pattern to give a value
that is in some way related to the original's meaning.

To give a simple example of the difference:

var = '2'  # the character two

castVar = ord(2)    # treat the character as an int
convVar = int(var)  # convert '2' to 2

casting is often done in C/C++ because it is common to return
a pointer to a piece of data and the programmer has to tell the compiler 
how to treat that data. It looks like a conversion so
casting is often mistaken for conversion. But they are not
the same. (And C++ confused the issue further by introducing
"dynamic casts" which often are conversions!)

Sorry to be picky but failing to understand the difference
has caused many a bug in C programs and I've suffered enough
pain to be sensitive! :-)

> Seems a backwards step unless (and am sure this is the case)it is
> beneficial in so many other ways?

Peter has explained why the v2 Python input() was a bad idea.
Explicit conversion is much safer.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list