[Tutor] 0 > "0" --> is there a "from __future__ import to make this raise a TypeError?

Albert-Jan Roskam sjeik_appie at hotmail.com
Tue Oct 13 09:30:56 EDT 2015


From: oscar.j.benjamin at gmail.com
Date: Mon, 12 Oct 2015 20:37:43 +0000
Subject: Re: [Tutor] 0> "0" --> is there a "from __future__ import to make this raise a TypeError?
To: sjeik_appie at hotmail.com; tutor at python.org





On Mon, 12 Oct 2015 17:15 Albert-Jan Roskam  wrote:
Hi,

In Python 2 one can do silly apple-pear comparisons such as 0> "0".*) "CPython implementation detail: Objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address.". In Python3  this has been fixed (it raises a TypeError). Is there a way to emulate this behavior in Python 2?





Why do you want to? This kind of thing is nice to have when it's there by default since it can help pick up errors. When it's not there that's unfortunate which is why this was changed in Python 3 but it's not really essential.
Most of the time you won't mix strings and ints unless it's a mistake. Are you worried about making mistakes? Why not just test your code under Python 3 if so?
--


========

Hi Oscar

Yes, a justified fear of making mistakes. That is, a mistake has already occurred and I don't want it to happen again.
I made a comparison with data from two sources: a csv file (reference file) an sqlite database (test data). The csv module will always return str, unless one converts it. The test data were written to sqlite with pandas.to_sql, which (it seems) tries to be helpful by making INTs of everything that looks like ints. I chose sqlite because the real data will be in SQL server, and I hope this would mimic the behavior wrt None, NULL, nan, "", etc. 

Yesterday I already posted a class with a modified __cmp__ method. Not sure if it came through (I' ve had major problems with DMARC when I was still using yahoo, not yet sure about hotmail)

Regards,
Albert-Jan
 		 	   		  


More information about the Tutor mailing list