#๐Ÿ”’ set of frozensets has duplicate

15 messages ยท Page 1 of 1 (latest)

broken shuttle
#

I want to avoid having duplicate triangles in a list, so I'm trying to use a set.
I wonder why the following two frozensets are added to my set in the first place, as the hashes are invariant of the order:

print("set:", {
  frozenset([Triangle(P(0,0), P(0,1), P(0,2)), Triangle(P(0,1), P(0,3), P(0,2))]),
  frozenset([Triangle(P(0,2), P(0,1), P(0,0)), Triangle(P(0,1), P(0,3), P(0,2))])
})
  • P's __hash__ function returns hash((self.x, self.y))
  • Triangle's returns hash(frozenset([self.a, self.b, self.c])) (as I want it to be invariant to the order of the three points)
    I'd expect there to be just the first element in the resulting set.
mighty vectorBOT
#

@broken shuttle

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

burnt hearth
#

I think you need to define __eq__ as well as __hash__.

#

Post your code.

#

both for P and for Triangle

neon dock
#

(as I want it to be invariant to the order of the three points)
You could also just use a sorted tuple instead

broken shuttle
#
@dataclasses.dataclass
class P:
    x: int
    y: int
    label: str = ""

    def __repr__(self):
        return f"({self.x},{self.y})"

    def __hash__(self):
        return hash((self.x, self.y))


@dataclasses.dataclass
class Triangle:
    a: P
    b: P
    c: P

    def __repr__(self):
        return f"<{self.a},{self.b},{self.c}>"

    def __hash__(self):
        return hash(frozenset([self.a, self.b, self.c]))
broken shuttle
burnt hearth
#

๐Ÿ’

silent salmon
#

!d dataclasses.dataclass

mighty vectorBOT
#

@dataclasses.dataclass(*, init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False, weakref_slot=False)```
This function is a [decorator](https://docs.python.org/3/glossary.html#term-decorator) that is used to add generated [special method](https://docs.python.org/3/glossary.html#term-special-method)s to classes, as described below.

The [`dataclass()`](https://docs.python.org/3/library/dataclasses.html#dataclasses.dataclass) decorator examines the class to find `field`s. A `field` is defined as a class variable that has a [type annotation](https://docs.python.org/3/glossary.html#term-variable-annotation). With two exceptions described below, nothing in [`dataclass()`](https://docs.python.org/3/library/dataclasses.html#dataclasses.dataclass) examines the type specified in the variable annotation.

The order of the fields in all of the generated methods is the order in which they appear in the class definition.
silent salmon
#

Having a __hash__() implies that instances of the class are immutable

mighty vectorBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.