For an independent project, I wanted to do something like:
class Species(object):
As you can see, I really do not need more than one Species (id) instance for each identifier, but I will create it every time I create an Animal object with this id, and I would probably need several calls, for example, Animal(somename, 3) .
To solve this, I'm trying to make a class so that for two instances of it, say a and b, the following is always true:
(a == b) == (a is b)
This is what Python does with string literals and is called internship . Example:
a = "hello" b = "hello" print(a is b)
that print will give true (as long as the string is short enough if we use the python shell directly).
I can only guess how CPython does it (this is probably due to some C magic), so I am making my own version. So far I have:
class MyClass(object): myHash = {}
My questions:
a) Is my problem even worthy of a solution? Since my intended Speces object should be quite light and the maximum number of times that Animal can be called quite limited (imagine a Pokemon game: no more than 1000 copies, vertices)
b) If so, is this the right approach to solve my problem?
c) If this is not valid, could you talk about a simpler / cleaner / more pythonic way to solve this problem?