0

I have some problems with my code to work with both python 2 and python 3 when compare tuple with integer

I understand from python3 tuple cannot be compared with other data types directly, e.g., integer

However, in python 2, the following code is working

>>> t=(0,0)
>>> t > 1
True
>>> t < 1
False

In python 3, it fails with error tuple cannot be compared with int.

Any hints on how to make this work with both python 2 and python 3?

tudou
  • 467
  • 1
  • 7
  • 20
  • 2
    What is your intent with comparing a tuple to an int? What are you trying to do? – Carcigenicate Jul 22 '19 at 17:02
  • The python 2 behaviour is pretty undesirable although possible to re-create (I'll find the dupe). I think you shouldn't be comparing different types and expecting an answer – Chris_Rands Jul 22 '19 at 17:03
  • Are you trying to get the length or the sum of the tuple? – Pedro Lobito Jul 22 '19 at 17:03
  • @Chris_Rands My 2¢, but based on the OP rep, I don't think he'll be clarified by the dupe answer. – Pedro Lobito Jul 22 '19 at 17:06
  • Given that `(0, 0)` is a literal expression of a built-in type, `1` is a literal expression of a built-in type it follows that `t = (0,0); t > 1` **cannot** be customized in **any** way. There is no "magic method" to change. Now, your is cleary an XY problem. What are you trying to do that you need to make a non-sensical comparison between a tuple and an int? IMHO if you are trying to `.sort` an heterogeneous list you should always specify a `key=` parameter that defines the logic to use. This works in both python2 & 3 and avoids any nonsense like `(0,0) > 1 True`- – Bakuriu Jul 22 '19 at 17:06
  • I am working on an old code repo to make it compatible with both python 2 and python3 and noticed this difference in python 2 / python3 If the behavior of comparing a tuple and int is not desirable in python2, what this actual mean when (0,0) > 1 evaluates to True? – tudou Jul 22 '19 at 17:06
  • @PedroLobito Well the dupe answers the question, but yeh, it's probably an XY problem anyway – Chris_Rands Jul 22 '19 at 17:08
  • @tudou, re: "what this actual mean when (0,0) > 1 evaluates to True?". According to https://docs.python.org/2.7/reference/expressions.html#value-comparisons, "the choice whether one object is considered smaller or larger than another one is made arbitrarily but consistently within one execution of a program." In other words, (0,0) being greater than 1 doesn't "mean" anything. You can't even be sure that the result will be the same the next time you run your program. – Kevin Jul 22 '19 at 17:12
  • @Chris_Rands, thanks for your commets, but that is NOT what I am looking for. Bakuriu, thanks for the explaination. I total agreed this is what likely happens in python2 – tudou Jul 22 '19 at 17:12
  • @Kevin thanks for the comments, i am not sure if it is arbitrary, I tested with different integer values and in different test runs and "compared with" (0,0) > x, it is always True, (0,0) < x is always False. Anyway, I think this is should be a bug that needs to be addressed in my code repo thanks – tudou Jul 22 '19 at 17:15

0 Answers0