Division oddity

Michael Hudson mwh at python.net
Mon Jan 12 13:09:23 CET 2004

python at rcn.com (Raymond Hettinger) writes:

> [Tim Rowe]
> > > If I do from __future__ import division then eval(1/2) gives me 0.5 as
> > > expected. But if I do print input("enter a sum: ") and enter 1/2 as
> > > the sum I get 0 as if I hadn't done the import.  I thought input was
> > > supposed to give the same behaviour as an eval on raw input -- why the
> > > difference here?
> [Paul Rubin[
> > The input function is calling eval from the context of the module
> > where 'input' itself is defined.  If you use "from __future__ import
> > division" in module A and have "print 3/2" in module B, the value of
> > 3/2 in module B shouldn't be affected by the input, since module B
> > may depend on integer division having the old behavior.
> Right!
> So, the way to get eval() to respond to the import is to pass along
> the current environment:
> >>> from __future__ import division
> >>> eval('9/2', globals())
> 4.5

Um, that's not how it works.  Off the top of my head I'm not entirely
sure why eval() respects future statements and input() does not, but
it's probably easy enough to fix, if anyone cares (I don't think I


  Windows 2000: Smaller cow. Just as much crap.  -- Jim's pedigree of
  operating systems, asr

More information about the Python-list mailing list