CKTIS / Python
Keyword class
The class body runs immediately in a new namespace and the name receives the result of applying decorators, which can replace the class.
Version: Python 3.14; repository image, production runtime not attested
Syntax
class Child(Base):
...The class body runs immediately in a new namespace and the name receives the result of applying decorators, which can replace the class.
Since Python 3.12 class Box[T] introduces a type parameter. An ordinary method does not look up bare names in the class namespace, so it accesses class attributes through the instance or class. Class attributes are shared unless an instance shadows them. Methods receive the instance through normal descriptor binding.
A mutable list placed in the class body is shared across instances until a separate instance attribute is assigned.
Examples
class Shelf:
labels = []
def add(self, text):
self.labels.append(text)
a, b = Shelf(), Shelf()
a.add("A")
b.labels = []
b.add("B")
print(a.labels, b.labels, Shelf.labels)
marker = "module"
trace = []
@lambda cls: (cls, "wrapped")
class Probe[T]:
trace.append("class body")
marker = "class"
def read(self):
return marker, self.marker
original, tag = Probe
assert trace == ["class body"] and tag == "wrapped"
assert original.__type_params__[0].__name__ == "T" and original().read() == ("module", "class")
Result
['A'] ['B'] ['A']
A mutable list placed in the class body is shared across instances until a separate instance attribute is assigned.