I'm using the Python libraries for Google App Engine. How can I override the equals()
method on a class so that it judges equality on the user_id
field of the following class:
class UserAccount(db.Model):
# compare all equality tests on user_id
user = db.UserProperty(required=True)
user_id = db.StringProperty(required=True)
first_name = db.StringProperty()
last_name = db.StringProperty()
notifications = db.ListProperty(db.Key)
Right now, I'm doing equalty by getting a UserAccount
object and doing user1.user_id == user2.user_id
. Is there a way I can override it so that 'user1 == user2' will look at only the 'user_id' fields?
Thanks in advance
Override operators __eq__
(==) and __ne__
(!=)
e.g.
class UserAccount(db.Model):
def __eq__(self, other):
if isinstance(other, UserAccount):
return self.user_id == other.user_id
return NotImplemented
def __ne__(self, other):
result = self.__eq__(other)
if result is NotImplemented:
return result
return not result
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With