MCPcopy Index your code
hub / github.com/faif/python-patterns / __new__

Method __new__

patterns/structural/flyweight.py:39–48  ·  view source on GitHub ↗
(cls, value: str, suit: str)

Source from the content-addressed store, hash-verified

37 _pool: weakref.WeakValueDictionary = weakref.WeakValueDictionary()
38
39 def __new__(cls, value: str, suit: str):
40 # If the object exists in the pool - just return it
41 obj = cls._pool.get(value + suit)
42 # otherwise - create new one (and add it to the pool)
43 if obj is None:
44 obj = object.__new__(Card)
45 cls._pool[value + suit] = obj
46 # This row does the part we usually see in `__init__`
47 obj.value, obj.suit = value, suit
48 return obj
49
50 # If you uncomment `__init__` and comment-out `__new__` -
51 # Card becomes normal (non-flyweight).

Callers

nothing calls this directly

Calls 1

getMethod · 0.45

Tested by

no test coverage detected