When one needs a custom property, the easy, readable, and that already works way to create then is just to create an instance of the their class and assign the instance to the desired class attribute. All the class have to do is to have the methods that identify it as a descriptor: "__get__" and "__set__" for data descriptors , and optionally "__del__".
The class can be made in a way to make it easier to write the setters and getter , and have their "__set__" and "__get__" call these other methods, but that is about it. ``` class ThresholdProperty:
def __init__(self, threshold=0.5): self.threshold = threshold def __set_name__(self, owner, name): self.name = name def __set__(self, instance, value): self.instance.setattr("_" + self.name, value) def __get__(self, instance, owner): if instance is None: return self return int(getattr(instance, "_" + self.name) > self.threshold)
class Neuron: activation = ThresholdProperty()
On Tue, 4 Aug 2020 at 05:16, Chris Angelico email@example.com wrote:
On Tue, Aug 4, 2020 at 6:09 PM firstname.lastname@example.org wrote:
Seems like this topic was previously raised, but what if we add
possibility to decorate non function properties in class:
class Neuron: @linear_activation activation
What would that decoration do, exactly?
ChrisA _______________________________________________ Python-ideas mailing list -- email@example.com To unsubscribe send an email to firstname.lastname@example.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://email@example.com/message/V3HHI4... Code of Conduct: http://python.org/psf/codeofconduct/