[Python-Dev] Detecting tp_compare / tp_richcompare from Python

Tres Seaver tseaver at palladion.com
Thu Nov 8 21:31:51 CET 2012


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 11/08/2012 02:51 PM, Tres Seaver wrote:
> On 11/08/2012 12:59 PM, Guido van Rossum wrote:
>> In Python, you can write
>>> if C.__eq__ == object.__eq__: print('class C does not override 
>> __eq__')
>>> Does that help?
>> That works in Python3, but not in Python2 -- methodwrappers don't
> compare equal the same way slotwrappers do :(. I still need to
> straddle (back to Python 2.6.

I think I'm going to need two separate checkers:
- - In Py3k, use your check above for both '__eq__' and '__lt__', e.g:
 def check_comparable(key);
 klass = type(key)
 if klass.__eq__ == object.__eq__ or klass.__lt__ == object.__lt__:
 raise TypeError('Incomparable')
- - In Python2, I think I can just check that the instance (not its class)
 has either ('__lt__' and '__eq__') *or* '__cmp__', e.g.:
 def check_comparable(key);
 if not ((getattr(key, '__eq__', None) is not None and
 getattr(key, '__lt__', None) is not None) or
 getattr(key, '__cmp__', None) is not None):
 raise TypeError('Incomparable')
Tres.
- -- 
===================================================================
Tres Seaver +1 540-429-0999 tseaver at palladion.com
Palladion Software "Excellence by Design" http://palladion.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://www.enigmail.net/
iEYEARECAAYFAlCcFrcACgkQ+gerLs4ltQ6JDgCeJpDmo70H9Xgz3preNHVvaTl5
dYkAoNS+fpSXwZiaD2J2ONyQZ2mPIcFC
=OiMY
-----END PGP SIGNATURE-----


More information about the Python-Dev mailing list

AltStyle によって変換されたページ (->オリジナル) /