23 lines
578 B
Python
23 lines
578 B
Python
# tag::WilyDict[]
|
|
class WilyDict(dict):
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
self.__next_value = 0
|
|
|
|
def __missing__(self, key):
|
|
if key.startswith('__') and key.endswith('__'):
|
|
raise KeyError(key)
|
|
self[key] = value = self.__next_value
|
|
self.__next_value += 1
|
|
return value
|
|
# end::WilyDict[]
|
|
|
|
# tag::AUTOCONST[]
|
|
class AutoConstMeta(type):
|
|
def __prepare__(name, bases, **kwargs):
|
|
return WilyDict()
|
|
|
|
class AutoConst(metaclass=AutoConstMeta):
|
|
pass
|
|
# end::AUTOCONST[]
|