LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-28-2017, 05:43 PM   #1
rachelrose
LQ Newbie
 
Registered: Sep 2017
Posts: 6

Rep: Reputation: Disabled
Hash function in Python


I have the following Person class with the rich comparisons implemented. I'll keep my code short and relevant. Given the equals method:

Code:
 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())
how would I write a robust hash function? I've read the documentation on the official website, but it was discussed on SO as having problems

Last edited by rachelrose; 09-28-2017 at 06:30 PM.
 
Old 09-28-2017, 06:47 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: Slackware
Posts: 7,632

Rep: Reputation: 2949Reputation: 2949Reputation: 2949Reputation: 2949Reputation: 2949Reputation: 2949Reputation: 2949Reputation: 2949Reputation: 2949Reputation: 2949Reputation: 2949
Why do you think you need a custom hash function at all?
 
Old 09-28-2017, 06:59 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,240

Rep: Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406
Quote:
Originally Posted by millder on StackOverflow
The problem with this solution is that the hash(A(a, b, c)) == hash((a, b, c)). In other words, the hash collides with that of the tuple of its key members.
Sounds like a non-problem to me. Non-equal objects will sometimes have equal hashes, that is the nature of hashes.

Also, it seems a bit strange that you don't base equality on the identification numbers alone. Why would you have an identification_number otherwise?
 
Old 09-28-2017, 07:04 PM   #4
rachelrose
LQ Newbie
 
Registered: Sep 2017
Posts: 6

Original Poster
Rep: Reputation: Disabled
Hash function in Python

@dugan - According to the documentation, if I have an _eq_ function I should have a _hash_ function as well if I wanted to use it in a dictionary. From the documentation:

Quote:
If a class does not define an __eq__() method it should not define a __hash__() operation either; if it defines __eq__() but not __hash__(), its instances will not be usable as items in hashable collections.
...

Quote:
A class that overrides __eq__() and does not define __hash__() will have its __hash__() implicitly set to None. When the __hash__() method of a class is None, instances of the class will raise an appropriate TypeError when a program attempts to retrieve their hash value, and will also be correctly identified as unhashable when checking isinstance(obj, collections.Hashable).
@ntubski - I could of, but this is a hobby project, just wanted to experiment.

Last edited by rachelrose; 09-28-2017 at 07:09 PM.
 
1 members found this post helpful.
Old 10-03-2017, 08:50 AM   #5
rachelrose
LQ Newbie
 
Registered: Sep 2017
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ntubski View Post
Sounds like a non-problem to me. Non-equal objects will sometimes have equal hashes, that is the nature of hashes.

Also, it seems a bit strange that you don't base equality on the identification numbers alone. Why would you have an identification_number otherwise?
I managed to fix it. I used the hash example in the Python example, and modified it to fit my class.

Code:
def __hash__(self):
        return hash((self._iden, self._firstname.lower(), self._lastname.lower()))

Last edited by rachelrose; 10-03-2017 at 08:52 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I use base64 as a hash function? fantasy1215 Programming 8 01-13-2013 02:04 AM
hash function in C EnTe Programming 7 11-09-2010 01:48 PM
Fastest hash function Sanmayce Programming 0 11-09-2010 01:41 PM
Perl hash function from logfile eentonig Programming 7 08-02-2008 06:57 AM
hash function in kernel vishalbutte Programming 11 02-26-2006 03:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration