descriptor

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) >

Is the example of the descriptor protocol in the Python 3.6 documentation incorrect?

独自空忆成欢 提交于 2020-01-14 06:47:39
问题 I am new to Python and looking through its documentation I encountered the following example of the descriptor protocol that in my opinion is incorrect. . It looks like class IntField: def __get__(self, instance, owner): return instance.__dict__[self.name] def __set__(self, instance, value): if not isinstance(value, int): raise ValueError(f'expecting integer in {self.name}') instance.__dict__[self.name] = value # this is the new initializer: def __set_name__(self, owner, name): self.name =

Programmatically generate methods for a class

落花浮王杯 提交于 2020-01-12 14:13:10
问题 I have about 20 methods to redirect to a wrapper method that takes the original method, and the rest of the arguments: class my_socket(parent): def _in(self, method, *args, **kwargs): # do funky stuff def recv(self, *args, **kwargs): return self._in(super().recv, *args, **kwargs) def recv_into(self, *args, **kwargs): return self._in(super().recv_into, *args, **kwargs) # and so on... How can I add more of these methods programmatically? This is about as far as I get before everything starts to

Can a python descriptor be used to instantiate an attribute in the __init__ of another class?

自作多情 提交于 2020-01-03 05:22:08
问题 Or does the attribute have to be defined outside of any class methods? So my descriptor object is this. The IDN object already has some information about the UserNameField, so I want to use it. class UserNameElement(basePageElement): _testMethodName="UserNameElement Test method" def __init__(self, IDN, PTF): print "creating UserNameElement" self.locator = IDN.UserNameField() And here is my calling class. Where I want to instantiate the UserNameElement object class LoginPageObject

Training of SVM classifier using SIFT features

徘徊边缘 提交于 2019-12-29 07:17:30
问题 please i like to classify a set of image in 4 class with SIFT DESCRIPTOR and SVM. Now, using SIFT extractor I get keypoints of different sizes exemple img1 have 100 keypoints img2 have 55 keypoints.... how build histograms that give fixed size vectors with matlab 回答1: In this case, perhaps dense sift is a good choice. There are two main stages: Stage 1 : Creating a codebook. Divide the input image into a set of sub-images. Apply sift on each sub-image. Each key point will have 128 dimensional

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

creating a class that behaves like any variable but has callback on change/read

拈花ヽ惹草 提交于 2019-12-23 12:42:31
问题 I would like to create a class that behaves as a python variable but calls some callback function when the "variable" is changed/read. In other words, I'd like to be able to use the class as follows: x=myClass(change_callback, read_callback) defines x as an instance of myclass. The constructor ( INIT ) takes 2 functions as paramater: a function to be called each time x is changed, and a function to be called each time x is "read" The following statement: x=1 would "save" 1 and trigger a call

descriptor '__init__' of 'super' object needs argument

こ雲淡風輕ζ 提交于 2019-12-22 04:13:15
问题 I'm trying my hand at making an Object-Oriented text-based game in Python, and attempting to implement my first properties and decorators. Using the chapter 5 in the book 'Python 3 Object Oriented Programming', I've tried to use the examples and concepts discussed to get the following code to set a Game-object's 'current_room' property upon instantiation: class Room(object): ''' An area of the game's map.''' def __init__(self): print("Accessing the Room __init__ method.") class FirstRoom(Room

Are unbound descriptors possible?

試著忘記壹切 提交于 2019-12-21 23:34:39
问题 (and if not, is there an approach that accomplishes a similar thing?) For example, here's an example of a "bound" descriptor: class VerboseDescriptor(object): def __init__(self, init): self.val = init def __get__(self, obj, typ=None): print("[%s] Get is %s" % (obj, self.val)) return self.val def __set__(self, obj, val): print("[%s] Set to %s" % (obj, val)) self.val = val class Holder(object): def __init__(self, name): self.name = name def __repr__(self): return "Holder(%s)" % self.name val =