[Python-checkins] cpython: inspect: Micro-optimize __eq__ for Signature, Parameter and BoundArguments

yury.selivanov python-checkins at python.org
Fri May 15 00:23:01 CEST 2015


https://hg.python.org/cpython/rev/f0b10980b19e
changeset:   96056:f0b10980b19e
parent:      96054:15701e89d710
user:        Yury Selivanov <yselivanov at sprymix.com>
date:        Thu May 14 18:20:01 2015 -0400
summary:
  inspect: Micro-optimize __eq__ for Signature, Parameter and BoundArguments

Provide __ne__ method for consistency.

files:
  Lib/inspect.py |  32 ++++++++++++++++++++++----------
  1 files changed, 22 insertions(+), 10 deletions(-)


diff --git a/Lib/inspect.py b/Lib/inspect.py
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -2353,11 +2353,15 @@
         return hash((self.name, self.kind, self.annotation, self.default))
 
     def __eq__(self, other):
-        return (issubclass(other.__class__, Parameter) and
-                self._name == other._name and
-                self._kind == other._kind and
-                self._default == other._default and
-                self._annotation == other._annotation)
+        return (self is other or
+                    (issubclass(other.__class__, Parameter) and
+                     self._name == other._name and
+                     self._kind == other._kind and
+                     self._default == other._default and
+                     self._annotation == other._annotation))
+
+    def __ne__(self, other):
+        return not self.__eq__(other)
 
 
 class BoundArguments:
@@ -2441,9 +2445,13 @@
         return kwargs
 
     def __eq__(self, other):
-        return (issubclass(other.__class__, BoundArguments) and
-                self.signature == other.signature and
-                self.arguments == other.arguments)
+        return (self is other or
+                    (issubclass(other.__class__, BoundArguments) and
+                     self.signature == other.signature and
+                     self.arguments == other.arguments))
+
+    def __ne__(self, other):
+        return not self.__eq__(other)
 
     def __setstate__(self, state):
         self._signature = state['_signature']
@@ -2663,8 +2671,12 @@
         return hash((params, kwo_params, return_annotation))
 
     def __eq__(self, other):
-        return (isinstance(other, Signature) and
-                self._hash_basis() == other._hash_basis())
+        return (self is other or
+                    (isinstance(other, Signature) and
+                     self._hash_basis() == other._hash_basis()))
+
+    def __ne__(self, other):
+        return not self.__eq__(other)
 
     def _bind(self, args, kwargs, *, partial=False):
         """Private method. Don't use directly."""

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list