The task is as follows:

  1. there is a class that has 4 properties, 2 of which are read-only

  2. you need to create a second class (in some way dict-like), consisting of a sequence of instances of the first class and in which the composite key of the above two readonly properties should be used. How to make it better?

Hope explained more or less clear

I tried to do it by implementing the __eq__ and __hash__ methods for the first class, and in the second one I kept instances of the first class in the set. Something like this: gist . But I don’t really like it: the creation of a set, of course, happens by the "composite key", but you cannot get a value by this key

Looked at the collections library, but also did not find the right one.

  • Can you give an example of how the second class will be used? - Pavel
  • @Pavel In general, it will be necessary to implement in the second class, for example, the following methods: obtaining an element by a composite key; Search for items by one or both properties included in the key; deleting items by key; you will need to be able to stack instances of the first and second class. - 111
  • For example, there is a class Obj with fields key1, key2, val1, val2, and the second class is called Seq. Does it have to work like this pastebin.com/mAw460JZ ? - Pavel
  • @Pavel Yes, you can provide the necessary functionality in this way. Now I have already solved the problem in a certain way - you can look below - 111

1 answer 1

It was decided something like this:

 class Trp: def __init__(self, prefix, name, value, comment): self.prefix = prefix # readonly self.name = name # readonly self.value = value self.comment = comment class TrpStr: def __init__(self, *trps): self._trps = {} for trp in trps: self._trps.update({(trp.prefix, trp.name): trp}) 

The solution, of course, is not perfect.