Gearoid Murphy
Gearoid Murphy

Reputation: 12116

Python evaluates comparison of integer and character with the same value as False

I ran into this interesting little 'gotcha' this evening, consider the following code snippet:

( chr(1) == 1 )

This comparison evaluates to False on Python 2.7.4, is this a feature or a bug?, If a feature, can anyone explain the reasoning behind this design decision?

Upvotes: 1

Views: 392

Answers (1)

Pavel Anossov
Pavel Anossov

Reputation: 62898

There's no "char" type in python, chr returns a string of length one, and with strong typing it doesn't make sense for any string to be equal to any integer.

Upvotes: 5

Related Questions