python-descriptors

Type hinting with descriptors

心已入冬 提交于 2019-12-06 06:39:23
问题 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 Mypy. It looks like the correct usage is something like this: from typing import TypeVar T = TypeVar('T') V = TypeVar('V') class classproperty(): def __init__(self, getter: Callable[[Type[T], V]) -> None: self.getter = getter def __get__(self, instance:

Neat way to get descriptor object

人走茶凉 提交于 2019-12-04 03:37:59
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, SomeDescriptor) Is there better way to do it? I don't like this self.__class__.__dict__ stuff A.attr causes Python to call SomeDescriptor().__get__(None, A) so if you have SomeDescriptor.__get__ return self when inst is None , then A.attr will return the descriptor: class SomeDescriptor(): def __get__(self, inst, instcls): if inst is None: # instance attribute accessed on class, return self

python data and non-data descriptors

送分小仙女□ 提交于 2019-11-29 03:57:15
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 in place? After all, if I want to override an attribute in an instance dictionary, I already need to do that explicitely ( inst.__dict__["attr"] = val ), as a naive inst.attr = val would call the descriptor's __set__ method, which would (usually) not override the attribute in the instance dictionary. edit: just to make it clear, I understand what is

Why is this false? `SomeClass.method is SomeClass.method`

人盡茶涼 提交于 2019-11-28 01:38:21
Take this code for example: class SomeClass(): def a_method(self): pass print(SomeClass.a_method is SomeClass.a_method) # Example 1: False print(SomeClass.a_method == SomeClass.a_method) # Example 2: True print(SomeClass().a_method is SomeClass().a_method) # Example 3: False print(SomeClass().a_method == SomeClass().a_method) # Example 4: False Example 1: I would have guessed that they were the same object. Does Python make a copy of the method each time it is referenced? Example 2: Expected. Example 3: Expected, since they are different objects. Example 4: Why doesn't this output match

python data and non-data descriptors

你。 提交于 2019-11-27 22:12:50
问题 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 in place? After all, if I want to override an attribute in an instance dictionary, I already need to do that explicitely ( inst.__dict__["attr"] = val ), as a naive inst.attr = val would call the descriptor's __set__ method, which would (usually) not