Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6118389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:25:50+00:00 2026-05-23T15:25:50+00:00

Mark Ransom answered on a SO question about hashes here in SO: […] An

  • 0

Mark Ransom answered on a SO question about hashes here in SO:

[…] An
object is hashable if it has a hash value which never changes during
its lifetime
. So by the official definition, anything mutable can’t
be hashable, even if it has a __hash__() function. My statement
about both requirements being necessary is untrue, because being
hashable already implies the requirement to be immutable.

I want to make sure, that I got that right – even as a non native speaker – so I hope someone corrects me if I got it wrong.

Assuming this class

class Author(object):
    def __init__(self, id, name, age):
        self.id = id
        self.name = name
        self.age = age

    def __eq__(self, other):
        return self.id==other.id\
               and self.name==other.name

    def __hash__(self):
        return hash(('id', self.id,
                     'name', self.name))

I understand, that __eq__ allows me to compare objects of this class with the == operator. From Marks answer I understand, that even if my object peter = Author(1, "Peter", 33) has a __hash__ it is not hashable because I potentially could do something like peter.age = 43 which means it is not immutable. So my objects of the class Author are not hashable and therefore not usable as keys in dictionarys for example? Did I get right or does it seem, that I need more explanation? 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:25:51+00:00Added an answer on May 23, 2026 at 3:25 pm

    Instances of this class are hashable if you promise never to reset id or name on them. You can’t guarantee that these attributes will never be reset, by the Python principle that “we are all consenting adults here”, but it would be very bad style to offer methods that reset the attributes that __hash__ relies on.

    E.g., if you offer a set_name method on instances of Author, but no set_id, then clients of the class can reasonably presume that __hash__ operates only on the id.

    If you want to make it clear to clients of Author that they should not reset some attribute, you can label it private by prepending an _ to its name. If you then still want to offer (read-only) access to the attribute using its common name, you can make it a property:

    class Author(object):
        def __init__(self, id, name, age):
            self._id = id
            self._name = name
            self.age = age      # ages tend to change, so mutable
    
        id = property(lambda self: self._id)
        name = property(lambda self: self._name)
    
        def __eq__(self, other):
            return self.id==other.id\
                   and self.name==other.name
    
        def __hash__(self):
            return hash(('id', self.id,
                         'name', self.name))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Question mark in JavaScript I've seen this around a few times but
Possible Duplicate: Question mark equals, doesn't work on php <?php $a=123; ?> <?=$a?> This
Mark Up <%@ Page Language=C# AutoEventWireup=true CodeBehind=test.aspx.cs Inherits=Zuhaib.test %> <!-- Put IE into quirks
Mark Edington mentioned a Memory Manager called, SafeMM at the DelphiLive conference. I have
The mark in Vim takes you to your last cursor position. I want to
I'd like to mark a method as deprecated, so the people using it can
I need users to mark some section of text with custom tags, I can't
Consider the following mark-up input: * Line 1 * Line 2 :* Line 2.1
I'd like to mark some specific properties in a foreign assembly that i'm referencing
T = { {Name = Mark, HP = 54, Breed = Ghost}, {Name =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.