judy2k / stupid-python-tricks

Stupid Python tricks.
The Unlicense
144 stars 27 forks source link

Make ish support numbers #7

Closed snoack closed 9 years ago

snoack commented 9 years ago

Comparing an object againts a numeric value is quite a hassle in Python. Some objects compare smoothly (e.g 0 == 0.0), but others don't (e.g. "0" == 0, same when comparing an object implementing __int__ or __float__ to a number).

Also what should happen if you compare a float (probably being inaccurate anyway) with an integer? Doing an exact comparison of numbers just feels unnatural and is causing a lot of confusion.

I fixed this! With this PR all of the following evaluate to True:

'0' == 0-ish
0.1 == 0-ish
12 == 10-ish

from fractions import Fraction
Fraction('6/5') == 1-ish

class MyAwesomeIntishObject:
    def __int__(self):
        return 99
MyAwesomeIntishObject() == 100ish
judy2k commented 9 years ago

Keep going on like this, I'm going to have to add you to the committers list.