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
#6
Thank you for the explanation !
You helped me a lot.
I agree NamedTuple is useful here. But what I don't understand is the following sentence from the doc:
A set object is an unordered collection of distinct hashable objects.
And my class Building is Hashable. So why it needs to me immutable in order to be used in a set ? I don't find a clear reference.

So here the result that I have done and that is working. I will explain my aim in an other post as it would be different that the title of this post. My question of this post was only about why I cannot use a hashable object in a set.

from dataclasses import dataclass

@dataclass(frozen=True, eq=True)
class BuildingProperty:
    stone: int
    wood: int
    architecture: int
    decoration: int
    victory_point: int
    money: int


@dataclass(frozen=True, eq=True)
class Building:
    name: str
    properties: BuildingProperty


foo = Building(name="Test", properties=BuildingProperty(stone=2, wood=1, architecture=0, decoration=1, victory_point=8, money=4))
var = {foo}
Reply


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

Possibly Related Threads…
Thread Author Replies Views Last Post
  append str to list in dataclass flash77 6 594 Mar-14-2024, 06:26 PM
Last Post: flash77
  Sort a dict in dict cherry_cherry 4 81,432 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