| Class: classAttr | ./src/peak/binding/once.py | ||||||||
|---|---|---|---|---|---|---|---|---|---|
Class attribute bindingThis wrapper lets you create bindings which apply to a class, rather than to its instances. This can be useful for creating bindings in a base class that will summarize metadata about subclasses. Usage example:
class SomeClass(binding.Component):
CLASS_NAME = binding.classAttr(
binding.Make(
lambda self: self.__name__.upper()
)
)
class aSubclass(SomeClass):
pass
assert SomeClass.CLASS_NAME == "SOMECLASS"
assert aSubclass.CLASS_NAME == "ASUBCLASS"
Class attributes will only work in subclasses of classes like
Implementation note: class attributes actually cause a new metaclass to
be created on-the-fly to contain them. The generated metaclass is named
for the class that contained the class attributes, and has the same
assert SomeClass.__class__.__name__ == 'SomeClassClass'
assert aSubClass.__class__.__name__ == 'SomeClassClass'
Notice that the generated metaclass is reused for subsequent subclasses, as long as they don't define any new class attributes.
|