To add to the suggestions already given in this thread I dug into code I wrote 
some time ago.
Offered as an inspiration.

=== missing.py ===

from typing import Any

def MISSING(klass: Any) -> Any:
    """
    create a sentinel to indicate a missing instance of a class
    :param klass: the class of which an instance is missing
    :return: missing class object
    """
    g = globals()
    missing_klass_name = 
f"_MISSING_{klass.__module__}_{klass.__name__}_MISSING_"
    if missing_klass_name not in g:
        g[missing_klass_name] = type(
            missing_klass_name,
            (klass,),
            {
                "__repr__": lambda x: f"MISSING({klass.__name__})",
            }
        )
    return g[missing_klass_name]()

===

and as a demo:

=== demo_missing.py ===

import pickle

from missing import MISSING

x = MISSING(str)
y = "bar"
print(f"{x!r} == {y!r}: {x == y}")
print(f"{x!r} is {y!r}: {x is y}")
# MISSING(str) == 'bar': False
# MISSING(str) is 'bar': False

with open("object.pickled", "wb") as f:
    pickle.dump(x, f)
with open("object.pickled", "rb") as f:
    y = pickle.load(f)
print(f"{x!r} == {y!r}: {x == y}")
print(f"{x!r} is {y!r}: {x is y}")
# MISSING(str) == MISSING(str): True
# MISSING(str) is MISSING(str): False

def foo(a: int = MISSING(int), b: int = MISSING(int)):
    print(f"{a=} {isinstance(a, int)}")
    print(f"{b=} {isinstance(b, int)}")
    print(f"{a!r} == {b!r}: {a == b}")
    print(f"{a!r} is {b!r}: {a is b}")

foo()
# a=MISSING(int) True
# b=MISSING(int) True
# MISSING(int) == MISSING(int): True
# MISSING(int) is MISSING(int): False

foo(1)
# a=1 True
# b=MISSING(int) True
# 1 == MISSING(int): False
# 1 is MISSING(int): False

class Test:
    ...

t = MISSING(Test)
print(f"{t=}")
# t=MISSING(Test)

===
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/NABTH4G5IZZYCFSH2RBQNLQL3BKEUFKQ/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to