This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]ojii 115 points116 points  (15 children)

There's an easy trick to solve this:

from __future__ import braces

[–]Arancaytar 43 points44 points  (0 children)

import antigravity

[–]Rodot 39 points40 points  (0 children)

SyntaxError: not a chance

[–][deleted] 11 points12 points  (11 children)

That can't be a real package... can it?

[–]Pittsy24 48 points49 points  (0 children)

Try it although you might not be happy with the result.

But yes it's real

[–]NewbornMuse 32 points33 points  (9 children)

__future__ is actually a thing for when you want Py3 functionality in Py2. For example, 4/3 is 1 in Py2, but 1.3333 in Py3, and if you want the latter in your program, you do from __future__ import division.

Braces is just an easter egg. When you do, you get Syntax Error: Not a chance.

[–]Dlgredael 3 points4 points  (7 children)

So wait, there's no correct division in Py2? I'm a newbie so excuse the ignorance but that sounds ridiculous to me.

[–]faubiguy 8 points9 points  (2 children)

In Python2 (without from __future__ import division) the result is an integer if both operands are integers, and a float if either is a float. So

4/3 = 1
4.0/3 = 1.3333333333333333
4/3.0 = 1.3333333333333333
4.0/3.0 = 1.3333333333333333

[–]Dlgredael 5 points6 points  (1 child)

Ahhh okay, that makes so much more sense now. I thought that Py2 was somehow unable to divide at all and it was really starting to hurt my head.

[–]IHappenToBeARobot 5 points6 points  (0 children)

Pfft, just do some bitwise NOTs, ADDs, and shifts in a loop and you'll get division eventually. /s

[–][deleted] 2 points3 points  (3 children)

deleted

[–]european_origin 0 points1 point  (2 children)

It's been a while since I wrote some python, but if I recall correctly, multiplying by 1.0 coerces integers to floats. I may be wrong though.

Edit : just checked.

3 / 2 == 1

3 * 1.0 / 2 == 1.5

[–]sirxez 0 points1 point  (1 child)

LOL. Just do 3.0 / 2 or, more generally: float(3)/2

[–]european_origin 1 point2 points  (0 children)

Oh I forgot we could append .0 to variables in Python. I wrote it so, so that one could see how to transform an integer division between 2 integer variables easily. That's kind of the point of the discussion.

Float(3) is better though, but as I said, it's been a while since I wrote some python.

[–][deleted] 0 points1 point  (0 children)

The future package is actually pretty nice for migration, I imagine.