python-descriptors

How to freeze some arguments over multiple related class methods

為{幸葍}努か 提交于 2021-01-28 11:29:49
问题 What's the "best" method to take a collection of functions that use some common argument names (assumed to mean the same thing), and make an object that holds these functions, but with some key argument values fixed, or at least their defaults fixed. If I'm going to have a set of functions meant to work on some specific data defined by a set of attributes, I'd usually use a class, providing the attributes that must be set in the __init__ . But sometimes it makes more sense to start with

Writing a Non-Data Descriptor

偶尔善良 提交于 2020-03-22 05:09:07
问题 I am learning about descriptors in python. I want to write a non-data descriptor but the class having the descriptor as its classmethod doesn't call the __get__ special method when I call the classmethod. This is my example (without the __set__ ): class D(object): "The Descriptor" def __init__(self, x = 1395): self.x = x def __get__(self, instance, owner): print "getting", self.x return self.x class C(object): d = D() def __init__(self, d): self.d = d And here is how I call it: >>> c = C(4) >

Writing a Non-Data Descriptor

两盒软妹~` 提交于 2020-03-22 05:07:33
问题 I am learning about descriptors in python. I want to write a non-data descriptor but the class having the descriptor as its classmethod doesn't call the __get__ special method when I call the classmethod. This is my example (without the __set__ ): class D(object): "The Descriptor" def __init__(self, x = 1395): self.x = x def __get__(self, instance, owner): print "getting", self.x return self.x class C(object): d = D() def __init__(self, d): self.d = d And here is how I call it: >>> c = C(4) >

Setting a class __name__ declaratively

纵然是瞬间 提交于 2019-12-30 09:10:10
问题 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 it's in the dict 'not Potato' I thought maybe it was simply a case that this was overwritten after the class definition block completes. But seems that's not true, because the name is writable yet apparently not set in the class dict: >>> Potato.__name__ = 'no

Descriptors and direct access: Python reference

偶尔善良 提交于 2019-12-24 10:56:53
问题 The python 3.3 documentation tells me that direct access to a property descriptor should be possible, although I'm skeptical of its syntax x.__get__(a) . But the example that I constructed below fails. Am I missing something? class MyDescriptor(object): """Descriptor""" def __get__(self, instance, owner): print "hello" return 42 class Owner(object): x = MyDescriptor() def do_direct_access(self): self.x.__get__(self) if __name__ == '__main__': my_instance = Owner() print my_instance.x my

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

给你一囗甜甜゛ 提交于 2019-12-17 16:52:39
问题 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.

Python - How to pass instance variable value to descriptor parameter?

冷暖自知 提交于 2019-12-12 05:39:27
问题 Considering below case, ItemList Class: # filename: item_list.py # locators dictionary is intentionally placed outside `ItemList` class locators = { 'item_id': 'css=tr:nth-of-type({item_num}) .id', 'item_title': 'css=tr:nth-of-type({item_num}) .alert-rules', # other properties } class ItemList(Item): # --- these are data descriptors --- # getting item_id value based on its item_num item_id = TextReadOnly(locators['item_id'].format(item_num=self.item_num)) # getting item_title value based on

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

隐身守侯 提交于 2019-12-11 19:47:26
问题 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.

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

百般思念 提交于 2019-12-11 06:35:20
问题 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 = VocalDescriptor() B.v # prints "__get__, obj=None, objtype=<class '__main__.B'>" B.v = 3 # does not print "__set__", evidently does not trigger descriptor B.v # does not print anything, we overwrote the descriptor This question has an effective duplicate, but the duplicate was not answered,

Neat way to get descriptor object

安稳与你 提交于 2019-12-09 16:37:56
问题 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 回答1: 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():