descriptor

python resettable instance method memoization decorator

自闭症网瘾萝莉.ら 提交于 2019-11-27 16:32:01
问题 I'm attempting to build a decorator for an instance method of a class that will memoize the result. (This has been done a million times before) However, I'd like the option of being able to reset the memoized cache at any point (say, if something in the instance state changes, which might change the result of the method having nothing to do with its args). So, I attempted to build a decorator as a class instead of a function, so that I might have access to the cache as a class member. This

When and why might I assign an instance of a descriptor class to a class attribute in Python rather than use a property?

筅森魡賤 提交于 2019-11-27 14:44:52
问题 I'm aware that a property is a descriptor, but are there specific examples of when using a descriptor class might be more advantageous, pythonic, or provide some benefit over using @property on a method function? 回答1: Better encapsulation and re-usability: A descriptor class can have custom attributes set on instantiating. Sometimes it's useful to keep data confined in this manner, instead of having to worry about it getting set or overwritten on the descriptor's owner. 回答2: Let me quote from

How to visualize descriptor matching using opencv module in python

为君一笑 提交于 2019-11-27 11:48:42
问题 I am trying to use opencv with python. I wrote a descriptor (SIFT, SURF, or ORB) matching code in C++ version of opencv 2.4. I want to convert this code to opencv with python. I found some documents about how to use opencv functions in c++ but many of the opencv function in python I could not find how to use them. Here is my python code, and my current problem is that I don't know how to use "drawMatches" of opencv c++ in python. I found cv2.DRAW_MATCHES_FLAGS_DEFAULT but I have no idea how

Why is the Descriptor not getting called when defined as instance attribute?

冷暖自知 提交于 2019-11-26 23:21:35
问题 When I make the "data" variable a class variable, the following works, but when I make it an object variable, the descriptor is not called. Please help. class Data(object): products = { 'milk': {'price': 1.50, 'quantity': 10}, 'eggs': {'price': 0.20, 'quantity': 100}, 'cheese': {'price': 2.00, 'quantity': 10} } def __get__(self, obj, klas): print "Here in descriptor" return self.products class BusinessLogic(object): def __init__(self): # When I remove these 2 lines self.data = Data() #data =

Descriptors as instance attributes in python

China☆狼群 提交于 2019-11-26 22:46:05
问题 To the question: Why can't descriptors be instance attributes? it has been answered that: descriptor objects needs to live in the class, not in the instance because that is the way that the __getattribute__ is implemented. A simple example. Consider a descriptor: class Prop(object): def __get__(self, obj, objtype=None): if obj is None: return self return obj._value * obj._multiplier def __set__(self, obj, value): if obj is None: return self obj._value = value class Obj(object): val = Prop()

How to call a property of the base class if this property is being overwritten in the derived class?

試著忘記壹切 提交于 2019-11-26 22:04:17
I'm changing some classes of mine from an extensive use of getters and setters to a more pythonic use of properties. But now I'm stuck because some of my previous getters or setters would call the corresponding method of the base class, and then perform something else. But how can this be accomplished with properties? How to call the property getter or setter in the parent class? Of course just calling the attribute itself gives infinite recursion. class Foo(object): @property def bar(self): return 5 @bar.setter def bar(self, a): print a class FooBar(Foo): @property def bar(self): # return the

How to call a property of the base class if this property is being overwritten in the derived class?

笑着哭i 提交于 2019-11-26 08:59:48
问题 I\'m changing some classes of mine from an extensive use of getters and setters to a more pythonic use of properties. But now I\'m stuck because some of my previous getters or setters would call the corresponding method of the base class, and then perform something else. But how can this be accomplished with properties? How to call the property getter or setter in the parent class? Of course just calling the attribute itself gives infinite recursion. class Foo(object): @property def bar(self)

Understanding __get__ and __set__ and Python descriptors

…衆ロ難τιáo~ 提交于 2019-11-26 00:08:06
问题 I am trying to understand what Python\'s descriptors are and what they can be useful for. However, I am failing at it. 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): self.value = float(value) class Temperature(object): celsius = Celsius() Why do I need the descriptor class? What is instance