Questions tagged [python-descriptors]

Python descriptors are a way to create managed attributes. Among their many advantages, managed attributes are used to protect an attribute from changes or to automatically update the values of a dependant attribute.

169 questions
395
votes
8 answers

Understanding __get__ and __set__ and Python descriptors

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 =…
Matt Bronson
  • 3,969
  • 3
  • 15
  • 4
29
votes
2 answers

How to use super() with one argument?

While reading the Python documentation on super(), I stumbled on the following statement: If the second argument is omitted, the super object returned is unbound. What does ‘unbound’ mean and how to use super() with one argument?
user3282758
  • 1,379
  • 11
  • 29
25
votes
2 answers

Why is @staticmethod not preserved across classes, when @classmethod is?

Take the following example script: class A(object): @classmethod def one(cls): print("I am class") @staticmethod def two(): print("I am static") class B(object): one = A.one two =…
17
votes
2 answers

How can I get the attribute name when working with descriptor protocol in Python?

The descriptor protocol works fine but I still have one issue I would like to resolve. I have a descriptor: class Field(object): def __init__(self, type_, name, value=None, required=False): self.type = type_ self.name = "_" +…
Johan Vergeer
  • 5,208
  • 10
  • 48
  • 105
16
votes
3 answers

Why do people default owner parameter to None in __get__?

I've seen this quite often: def __get__(self, instance, owner=None): Why do some people use the default value of None for the the owner parameter? This is even done in the Python docs: descr.__get__(self, obj, type=None) --> value
Markus Meskanen
  • 19,939
  • 18
  • 80
  • 119
14
votes
3 answers

Why does setting a descriptor on a class overwrite the descriptor?

Simple repro: class VocalDescriptor(object): def __get__(self, obj, objtype): print('__get__, obj={}, objtype={}'.format(obj, objtype)) def __set__(self, obj, val): print('__set__') class B(object): v =…
Michael Carilli
  • 371
  • 2
  • 12
13
votes
3 answers

Type hinting with descriptors

In this pull request it looks like type hinting support for descriptors was added. However it looks like no finalized "correct" usage example was ever posted, nor does it looks like any documentation was ever added to the typing module or to…
shadowtalker
  • 12,529
  • 3
  • 53
  • 96
13
votes
1 answer

Setting a class __name__ declaratively

Why can't you override a class name declaratively, e.g. to use a class name which is not a valid identifier? >>> class Potato: ... __name__ = 'not Potato' ... >>> Potato.__name__ # doesn't stick 'Potato' >>> Potato().__name__ # .. but…
wim
  • 338,267
  • 99
  • 616
  • 750
13
votes
1 answer

python data and non-data descriptors

According to Python's documentation, Data descriptors with __set__() and __get__() defined always override a redefinition in an instance dictionary. I have no problem understanding this sentence, but can someone clarify for me why such a rule is…
antony
  • 2,877
  • 4
  • 31
  • 43
12
votes
5 answers

How to assign member variables temporarily?

I often find that I need to assign some member variables temporarily, e.g. old_x = c.x old_y = c.y # keep c.z unchanged c.x = new_x c.y = new_y do_something(c) c.x = old_x c.y = old_y but I wish I could simply write with c.x = new_x; c.y =…
MWB
  • 11,740
  • 6
  • 46
  • 91
11
votes
2 answers

Neat way to get descriptor object

In Python 3 class A(object): attr = SomeDescriptor() ... def somewhere(self): # need to check is type of self.attr is SomeDescriptor() desc = self.__class__.__dict__[attr_name] return isinstance(desc,…
Antigluk
  • 1,176
  • 2
  • 11
  • 30
10
votes
1 answer

mypy error: Callable has no attribute "__get__"

I have something like the following: from typing import TypeVar, Callable, Generic, Type, Union, Optional T = TypeVar("T") V = TypeVar("V") class DescClass(Generic[T, V]): """A descriptor.""" def __init__(self, func: Callable[[T], V]) ->…
Rick
  • 43,029
  • 15
  • 76
  • 119
9
votes
1 answer

Using Typing and Mypy with Descriptors

I have looked at a few SO posts and github issues related to using Typing with descriptors but I have not been able to get my issue resolved. I have wrapper classes and I want to define properties as descriptos that can get and "cast" properties of…
gtalarico
  • 4,409
  • 1
  • 20
  • 42
7
votes
1 answer

typing: How to bind owner class to generic descriptor?

Can I implement a generic descriptor in Python in a way it will support/respect/understand inheritance hierarchy of his owners? It should be more clear in the code: from typing import ( Generic, Optional, TYPE_CHECKING, Type, TypeVar, Union,…
NobbyNobbs
  • 1,341
  • 1
  • 11
  • 17
6
votes
1 answer

Class properties in Python 3.11+

In Python 3.9, we gained the ability to chain @classmethod and @property to sensibly create class properties. class Foo: @property def instance_property(self): return "A regular property" @classmethod @property def…
1
2 3
11 12