0

How can I check how standard classes (e.g. socket) implement their rich comparisons (e.g. __eq__) in Python3?

I'm learning Python and have implemented a web server in the language.

I'm not sure whether it's safe to compare sockets by == to determine if the listening server socket s is part of the read list returned by select.select().

I've consulted the documentation for sockets (see below), but a search for "equal" doesn't show anything.

Is there a general rule that rich comparisons are left unimplemented unless otherwise stated? If so, how is comparison performed?

References:

https://docs.python.org/2/reference/datamodel.html#object.cmp https://docs.python.org/3/library/socket.html

Shuzheng
  • 11,288
  • 20
  • 88
  • 186
  • `__cmp__` doesn't exist in Python 3, which version are you actually using? – jonrsharpe Jul 08 '19 at 06:51
  • @jonrsharpe - I'm using Python2/3. I maybe restate my question to "how to determine the implementation of equality for Python classes ". – Shuzheng Jul 08 '19 at 07:10
  • 1
    That's almost deliberately not an answer. Do you mean you're writing code that supports both 2 *and* 3 (and at [this point](https://pythonclock.org/), frankly, *why*)? If you want to restate your question, [edit] it. – jonrsharpe Jul 08 '19 at 07:13
  • @jonrsharpe - I've edited thanks. Because some code is still Python2 and I need to edit it. – Shuzheng Jul 08 '19 at 07:19

1 Answers1

1

I'd ask Python what __eq__ is, e. g.:

>>> socket.socket.__eq__
<slot wrapper '__eq__' of 'object' objects>

From this, I'd conclude that the socket module doesn't implement its own rich comparison for a socket object, but rather uses the general method for object objects.

Perhaps you might also want to read about Python method-wrapper type?

By default, eq compares objects by their address (id()), right? How do I know this C-implemented function does that?

To know for sure, you can look at the source; see What is the source code of hash() and eq() of object in Python?

Armali
  • 18,255
  • 14
  • 57
  • 171
  • Thanks. `` is a C-implemented function. By default, `__eq__` compares objects by their address (`id()`), right? How do I know this C-implemented function does that? – Shuzheng Jul 09 '19 at 11:52
  • I hope the amendment above answers that question. – Armali Jul 10 '19 at 06:57