Fun with numbers - dammit, but I want a cast!

Richard Brodie R.Brodie at rl.ac.uk
Mon Aug 11 15:01:20 CEST 2003


"Graham Nicholls" <graham at rockcons.co.uk> wrote in message
news:3f378fac$0$10778$afc38c87 at auth.uk.news.easynet.net...

> I've tried doing img_x=1.0 to force it to be a float, but I'm getting a bit
> frustrated as it seems to make no difference - once image.getsize returns
> the (integer) value of the x size, it simply converts back to an int.

Yes. Types are properties of values, not names. There is no implied
declaration: you can assign a float to img_x then later an int,  string,
function or whatever.

To get floating point division you can use a cast like syntax, by using
the builtin float() function.

>>> 1 / 2
0
>>> float (1) / float(2)
0.5

In future versions of Python, / will become a floating point division operator.
// will be used for truncating division. For compatibility, you need to
explicitly enable the new behaviour at present, either with a from __future__
or a -Q option. These activate Guido's famous time machine, which add
new features to Python before you ask for them.

>>> from __future__ import division
>>> 1 / 2
0.5
>>> 1 // 2
0

U:\>python -Qwarn
ActivePython 2.2.2 Build 224 (ActiveState Corp.) based on
Python 2.2.2 (#37, Nov 26 2002, 10:24:37) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 1/2
__main__:1: DeprecationWarning: classic int division
0






More information about the Python-list mailing list