Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hash function in Python
#1
I've done some research and was able to implement the rich comparisons in my Person class. I have two questions,

1. Given the equals method how do I construct a good __hash__ function? I've read the documentation on the official website, but it was discussed on SO as having problems

def __init__(self, iden, firstname, lastname):
        self._firstname = firstname
        self._lastname = lastname
        self._iden = iden

    @property
    def identification_number(self):
        return self._iden

    @property
    def first_name(self):
        return self._firstname

    @property
    def last_name(self):
        return self._lastname

    def __eq__(self, other):
        return (self._iden, self._lastname.lower(), self._firstname.lower()) == (other.identification_number, other.last_name.lower(), other.first_name.lower())
2. When creating the other rich comparisons, is it good practice to check if the operand is an instance of the class? For example, take the following __lt__ function:
    def __lt__(self, other):
        if isinstance(other, Person):
            return (self._lastname.lower(), self._firstname.lower()) < (other.last_name.lower(), other.first_name.lower())
        return NotImplemented
If it's considered bad practice, how then would I return NotImplemented error? The documentation clearly specifies that rich comparisons should return this error if the methods used for comparison haven't been implemented.
Reply
#2
I don't see a problem there. Remember that if the two objects are equal, they must return the same hash. The problem SO seems to be talking about is two objects with the same hash that aren't equal. That's the reverse, and shouldn't really cause a problem, at least for standard Python use of the hash in dictionaries and sets. I would just construct the hash based on the tuple of the three attributes you are using for equality.

For the less than, that looks fine for handling the NotImplemented. That's how I always do it: if/elif to match different classes, and if no appropriate class then return NotImplemented. I am wondering why self and other have different attribute names, though.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Sep-29-2017, 02:21 PM)ichabod801 Wrote: I don't see a problem there. Remember that if the two objects are equal, they must return the same hash. The problem SO seems to be talking about is two objects with the same hash that aren't equal. That's the reverse, and shouldn't really cause a problem, at least for standard Python use of the hash in dictionaries and sets. I would just construct the hash based on the tuple of the three attributes you are using for equality.

For the less than, that looks fine for handling the NotImplemented. That's how I always do it: if/elif to match different classes, and if no appropriate class then return NotImplemented. I am wondering why self and other have different attribute names, though.

Thanks for you answer!

They have different property names because the other attributes are being called with the getters. For consistency, I should choose the getters approach and stick with it. PyCharm complains I'm attempting to access private variables when I call them directly.
Reply
#4
Oh, duh. I saw the @properties but didn't read them closely.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] How to crack hash with hashlib Milan 0 1,422 Mar-09-2023, 08:25 PM
Last Post: Milan
  Hash command works differently for me in CMD and Spyder ZweiDCG 3 2,360 Sep-10-2019, 01:10 PM
Last Post: DeaD_EyE
  length constraint on phrase hash to password javaben 0 1,922 Aug-21-2019, 05:34 PM
Last Post: javaben
  Create file archive that contains crypto hash ED209 1 2,066 May-29-2019, 03:05 AM
Last Post: heiner55
  Fastest dict/map method when 'key' is already a hash? tasket 6 4,008 Apr-20-2019, 06:40 PM
Last Post: tasket
  hash v2 and v3 help Normalitie 7 4,356 Mar-22-2018, 01:57 PM
Last Post: DeaD_EyE
  how to generate sha256 hash for each line of my txt file | using python version 3.6.4 rajtekken5 2 9,103 Feb-11-2018, 01:41 PM
Last Post: rajtekken5
  virtualenv activate.ps1 hash error po20 2 3,834 Jan-13-2018, 09:21 AM
Last Post: po20
  Python Hash list check here2learn 4 5,419 Feb-27-2017, 08:35 PM
Last Post: here2learn
  fast hash function verstapp 3 6,053 Dec-13-2016, 07:10 PM
Last Post: verstapp

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020