What's a good way to create a singleton type? #1311
Answered
by
erictraut
decorator-factory
asked this question in
Q&A
-
I'm not talking about a roundabout way of creating a global variable (the Singleton pattern), but rather a "sentinel" value, when TOO_LONG = object()
def _stack_divergence(old_stack: Stack, new_stack: Stack):
"""
>>> root = (2, 1, None)
>>> [*_stack_divergence( (3, root), ("b", ("a", root)) )]
[
('same', None, None ),
('same', (1, None), (1, None) ),
('same', (2, (1, None)), (2, (1, None)) ),
('different', (3, (2, (1, None))), ("a", (2, (1, None))) ),
('different', TOO_LONG, ("b", ("a", (2, (1, None)))) ),
]
"""
for (x, y) in zip_longest(
_stacks_in_reverse(old_stack),
_stacks_in_reverse(new_stack),
fillvalue=TOO_LONG
):
if x is y:
yield ("same", x, y)
else:
yield ("different", x, y) Using an Enum with a single member should work, but it isn't properly accounted for in narrowing (I assume it's a bug? #1309). |
Beta Was this translation helpful? Give feedback.
Answered by
erictraut
Dec 24, 2020
Replies: 1 comment 1 reply
-
I'm not 100% clear what you're trying to do, but I think it's related to this thread: python/typing#689. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
decorator-factory
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm not 100% clear what you're trying to do, but I think it's related to this thread: python/typing#689.