3131
3232
3333class _Singleton (type ):
34- """Metaclass to make the child, which should take zero initialization arguments , a singleton
34+ """Metaclass to make the child, which should take a single "const" argument , a singleton
3535 object."""
3636
37- def _get_singleton_instance (cls ):
38- return cls ._INSTANCE
37+ def _get_singleton_instance (cls , const = False ):
38+ return cls ._CONST_INSTANCE if const else cls . _INSTANCE
3939
4040 @classmethod
4141 def __prepare__ (mcs , name , bases ): # pylint: disable=unused-argument
@@ -45,6 +45,7 @@ def __prepare__(mcs, name, bases): # pylint: disable=unused-argument
4545 def __new__ (cls , name , bases , namespace ):
4646 out = super ().__new__ (cls , name , bases , namespace )
4747 out ._INSTANCE = object .__new__ (out ) # pylint: disable=invalid-name
48+ out ._CONST_INSTANCE = object .__new__ (out ) # pylint: disable=invalid-name
4849 return out
4950
5051
@@ -88,13 +89,18 @@ def __setstate__(self, state):
8889
8990
9091@typing .final
91- class Bool (Type ):
92+ class Bool (Type , metaclass = _Singleton ):
9293 """The Boolean type. This has exactly two values: ``True`` and ``False``."""
9394
94- __slots__ = ("const" , )
95+ __slots__ = ()
9596
96- def __init__ (self , * , const : bool = False ):
97- super (Type , self ).__setattr__ ("const" , const )
97+ def __new__ (cls , * , const = False ):
98+ return cls ._get_singleton_instance (const )
99+
100+ @property
101+ def const (self ):
102+ # Check if this instance is the const singleton.
103+ return self is self .__class__ ._CONST_INSTANCE
98104
99105 def __repr__ (self ):
100106 return f"Bool(const={ self .const } )"
0 commit comments