[Tutor] how come this doesnt work

Alan Gauld alan.gauld at btinternet.com
Sat Jan 15 13:07:00 CET 2011


"Alex Hall" <mehgcap at gmail.com> wrote

> m=int(m)
> just before the if statement. This causes m to turn from a string 
> into
> an integer and is what is known as "casting" or "type casting", if I
> have my vocabulary correct.

Sadly you don't although its a common error.

This is type conversion. You are actually changing the type of m.

Type casting is something very different and is not really available
in Python (except by underhand use of the struct module!). In type
casting you tell the intertpreter to treat the bit pattern of one 
variable
as if it were a different type but do not actually change the 
underlying
bit pattern. Its used a lot in low level languages such as C but is 
not
very useful in Python.


Sorry to be pedantic, and if you hadn't mentioned correct
vocabulary I probably wouldn't have bothered correcting it. :-)

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list