make User object hashable to allow use with sets
authormohammed chamma <hms.mohammed@gmail.com>
Tue, 24 Dec 2019 03:18:01 +0000 (22:18 -0500)
committerGitHub <noreply@github.com>
Tue, 24 Dec 2019 03:18:01 +0000 (22:18 -0500)
this allows for one-liners like
```connections = list(set(followers) & set(friends))```

tweepy/models.py

index 7c33d8f52453d2d4c8f54d0fac35493af18da013..aceb398427a29fb212cb68347da0247ee7ac9c5b 100644 (file)
@@ -224,7 +224,12 @@ class User(Model):
             return result
 
         return not result
-
+    
+    def __hash__(self):
+        if hasattr(self, 'id'):
+            return hash(self.id)
+        else:
+            raise TypeError('unhashable type: {} (no id attribute)'.format(type(self)))
 
 class DirectMessage(Model):