<jupyter_start><jupyter_text>Table of Contents<jupyter_code>class D:
    def __init__(self):
        self.classvars = {}
    def __set_name__(self, cls, name):
        self.name = name
    def __get__(self, instance, ownerclass):
        if instance:
            if isinstance(instance, type):
                return self.classvars[instance]
            return instance.__dict__.get(self.name, None)
        return ownerclass.__dict__[self.name]
    def __set__(self, instance, val):
        if isinstance(instance, type):
            self.classvars[instance] = val
        else:
            instance.__dict__[self.name] = val

class Class(type): 
    def __new__(cls, name, base, clsdict):
        for att in clsdict:
            if att.startswith('_'): continue
            if not isinstance(clsdict[att], D):
                raise TypeError("Attribute must be of type 'Att'")
        def fset(instance, att, val):
            #print("%s.__setattr__(fset)" % type(instance).__name__, instance, att, val)
            if not hasattr(type(instance), att):
                m = "Class instance '%s' has no Att '%s'"
                raise AttributeError(m % (instance, att))
            super(type(instance), instance).__setattr__(att, val)
        clsdict['__setattr__'] = fset
        return type.__new__(cls, name, base, clsdict)
    
    def __setattr__(cls, att, val):
        #print("Class.__setattr__", cls, att, val)
        if not hasattr(type(cls), att):
            m = "Class type '%s' has no Att '%s'"
            raise AttributeError(m % (cls, att))
        super().__setattr__(att, val)

class YT(Class, metaclass=Class):
    d = D()
    
class Y(metaclass=YT): pass
class X(metaclass=YT): 
    a = D()

Y.d = 1
X.d = 2
Y.d, X.d
YT.d.classvars
try:
    Y.x = 12
except AttributeError as e:
    print(e)
try:
    YT.a = 12
except AttributeError as e:
    print(e)
try:
    YT.d = 1
except AttributeError as e:
    print(e)
x = X()
try:
    x.z = 12
except AttributeError as e:
    print(e)
x.a = 12
x.a
try:
    class Z(metaclass=Class):
        i = 1
except TypeError as e:
    print(e)<jupyter_output>att must be of type 'D'
{'fraction_non_alphanumeric': 0.12406367041198502, 'fraction_numerical': 0.0056179775280898875, 'mean_word_length': 2.3131782945736434, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '377749', 'license': 'MIT', 'n_tokens_mistral': 721, 'n_tokens_neox': 705, 'n_words': 198, 'path': 'Class instance of Type.ipynb', 'repo_name': 'gitthious/ADT', 'size': '2136'}