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

  • SEARCH
  • Home
  • 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 6559805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:22:40+00:00 2026-05-25T13:22:40+00:00

A person asked a question on SO about how to get a list unique

  • 0

A person asked a question on SO about how to get a list unique function in python with an alternative equality function.

I was thinking it could be done by inheriting from the element class and overloading the equality function

import functools
@functools.total_ordering
class ffloat(float):
def __eq__(self,other):
    if floor(self) == floor(other):
        return True
    else:
        return False
def __le__(self,other):
    if self == other:
        return True
    else:
        return float(self) <= float(other)
def __hash__(self):
    return floor(self)


a = map(ffloat,[4.3,8,8.9, 13])


In [41]: a[1] == a[2]
Out[41]: True

but

In [42]: set(a)
Out[42]: set([4.3, 8.0, 8.9, 13.0])

Edit: replaced abs < 1.5 equality with floor equality

Added Hash
P.S. is there a way to make a class factory out of this that a class and two lambda and returns a class that inherits from the first one overriding the needed equality function.

  • 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-25T13:22:40+00:00Added an answer on May 25, 2026 at 1:22 pm

    This is not a valid equality function, since it’s not transitive:

    mfloat(0) == mfloat(1) == mfloat(2), but mfloat(0) != mfloat(2).

    Also note that in order to be used in a set, you must override __hash__ so that the following property holds for all instances a, b of your class:

    a == b ⇒ hash(a) == hash(b)
    

    set finds out that hash(mfloat(8)) != hash(mfloat(9)). Since set assumes the above property holds, it concludes that mfloat(8) != mfloat(9) without actually calling __eq__.


    In summary, this works:

    from math import floor
    class ffloat(float):
        def __eq__(self,other):
            return floor(self) == floor(other):
        def __hash__(self):
            return floor(self)
    
    a = map(ffloat,[4.3,8,8.9, 13])
    print(set(a))
    # output: {8.0, 4.3, 13.0}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just asked a question here, about why an iframe_resize function wasn't working. After
I've asked this question to the forums on the Mootools website and one person
I had an interview today and the person asked me this question: How do
I know that this question asked a lot (i think) but i could not
I just asked a rtm worthy question about SVN . I've used SVN for
Few weeks ago I asked a question on increasing the speed of a function
I initially asked a question about how to create linux users via HTML (essentially
I recently attended a interview in C# where i was asked a question about
hey I asked a question here Rails association help , this person directed me
I have a very similar situation to the person who asked: Can I serve

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.