python-descriptors

How do I tell pylint about a descriptor providing access to iterables, subscriptables?

How do I tell pylint about a descriptor providing access to iterables, subscriptables? Question: I have a decorator that I can use to mark a read-only class property: class class_ro_property(property): def __init__(self, getter:Callable): self._getter = getter def __get__(self, _, cls): return self._getter(cls) class MyClass: @class_ro_property def my_property(cls): return [1, 2, 3] The problem is that …

Total answers: 2

`__set_name__` hook manually added to `functools.wraps()` descriptor instance never called

`__set_name__` hook manually added to `functools.wraps()` descriptor instance never called Question: I’m trying to add a __set_name__ hook to the descriptor produced by functools.wraps inside a decorator, but it is never called and I don’t see any error messages: import functools def wrap(fn): """Decorator.""" @functools.wraps(fn) def w(*args, **kwargs): return fn(*args, **kwargs) # This never gets …

Total answers: 1

What is the proper way to use descriptors as fields in Python dataclasses?

What is the proper way to use descriptors as fields in Python dataclasses? Question: I’ve been playing around with python dataclasses and was wondering: What is the most elegant or most pythonic way to make one or some of the fields descriptors? In the example below I define a Vector2D class that should be compared …

Total answers: 3

Why is `__dict__` attribute of a custom Python class instance a descriptor of the class, instead of an actual attribute of the instances?

Why is `__dict__` attribute of a custom Python class instance a descriptor of the class, instead of an actual attribute of the instances? Question: From https://stackoverflow.com/a/44880260/156458 Note that the __dict__ attribute of custom Python class instances is a descriptor; the instance itself doesn’t have the attribute, it is the class that provides it (so type(instance).__dict__[‘__dict__’].__get__(instance) …

Total answers: 2

Understanding __get__ and __set__ and Python descriptors

Understanding __get__ and __set__ and Python descriptors Question: I am trying to understand what Python’s descriptors are and what they are useful for. I understand how they work, but here are my doubts. Consider the following code: class Celsius(object): def __init__(self, value=0.0): self.value = float(value) def __get__(self, instance, owner): return self.value def __set__(self, instance, value): …

Total answers: 8