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 692669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:40:28+00:00 2026-05-14T02:40:28+00:00

I create the following class: class Image(object): def __init__(self, extension, data, urls=None, user_data=None): self._extension

  • 0

I create the following class:

class Image(object):
    def __init__(self, extension, data, urls=None, user_data=None):
        self._extension = extension
        self._data = data
        self._urls = urls
        self._user_data = user_data
        self._hex_digest = hashlib.sha1(self._data).hexDigest()

Images should be equal when all values are equal. Therefore I wrote:

    def __eq__(self, other):
        if isinstance(other, Image) and self.__dict__ == other.__dict__:
            return True
        return False

    def __ne__(self, other):
        return not self.__eq__(other)

    def __lt__(self, other):
        return self.__dict__ < other.__dict__
    ...

But how should the __hash__ method look like? Equal Images should return equal hashes…

    def __hash__(self):
        # won't work !?!  
        return hash(self.__dict__)

Is the way I try to use __eq__, __ne__, __lt__, __hash__, ... recommend?

  • 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-14T02:40:29+00:00Added an answer on May 14, 2026 at 2:40 am

    Do you really need the images to be ordered? If not, I would drop the __lt__ method. For __hash__, remember that two unequal objects can have the same hash value, so you can just pick one of your attributes (or use a tuple of multiple attributes) to derive the hash code. Ex:

    def __hash__(self):
        return hash(self._hex_digest)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create an alias for a class name. The following syntax would
I have created UITableCellView class called NoteCell . The header defines the following: #import
I can create the following and reference it using area[0].states[0] area[0].cities[0] var area =
I want to create the following element: <exercises xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="mySchema.xsd"> If I use something
I am trying to create a table with the following: CREATE TABLE GTW_WORKFLOW_MON (
Given an Oracle table created using the following: CREATE TABLE Log(WhenAdded TIMESTAMP(6) WITH TIME
Imagine the following tables: create table boxes( id int, name text, ...); create table
Consider the following table: create table temp ( name int, a int, b int
I have the following function: CREATE FUNCTION fGetTransactionStatusLog ( @TransactionID int ) RETURNS varchar(8000)
I've a table [File] that has the following schema CREATE TABLE [dbo].[File] ( [FileID]

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.