Python Forum
Creating a set with dataclass and dict
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating a set with dataclass and dict
#4
micseydel, thank you for your kind help.
But I am not sure I understood your answer.
You speak about using set in set or dict key. That's not what I meant.
My problem is the line 11 of my code. I have a dataclass that is Hashable (frozen and eq to True), but I cannot use it as an element of my set.

import collections
from dataclasses import dataclass


@dataclass(frozen=True, eq=True, repr=False)
class Building:
    name: str
    property: dict


foo = Building(name="Test", property={"key": 10})
print(isinstance(foo, collections.Hashable))
var = {foo}
Output:
F:/Perso/Python/board-game-solver/les-batisseurs/src/test.py:12: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.9 it will stop working print(isinstance(foo, collections.Hashable)) Traceback (most recent call last): File "F:/Perso/Python/board-game-solver/les-batisseurs/src/test.py", line 13, in <module> var = {foo} File "<string>", line 3, in __hash__ TypeError: unhashable type: 'dict' True Process finished with exit code 1
Thank you again !
Reply


Messages In This Thread
RE: Creating a set with dataclass and dict - by hobbitdur - Jan-16-2020, 08:36 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  append str to list in dataclass flash77 6 712 Mar-14-2024, 06:26 PM
Last Post: flash77
  Sort a dict in dict cherry_cherry 4 90,999 Apr-08-2020, 12:25 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020