descriptor

Creating dynamic docstrings in Python descriptor

人走茶凉 提交于 2019-12-21 09:33:06
问题 I am trying to generate some class definitions dynamically (for wrapping a C++ extension). The following descriptor works fine except when I try to access the docstring for a field using help(), it gives default documentation for the descriptor rather than the field it self. However when I do help(classname), it retrieves the docstring passed to the descriptor: class FieldDescriptor(object): def __init__(self, name, doc='No documentation available.'): self.name = name self.__doc__ = doc def _

How to call methods on Python class descriptor objects?

血红的双手。 提交于 2019-12-21 05:24:10
问题 I created a class String() with __get__() , __set__() , and a method to_db() ; however, when I do name = String() , I can't do self.name.to_db() because it's calling to_db() on the value returned by __get__() , not the object " name ". class String(object): def __init__(self, value=None): if value: self.value = str(value) def __get__(self, instance, owner): return self.value def __set__(self, instance, value): self.value = str(value) def to_db(self): return {'type':'string', 'value': self

How to create decorator for lazy initialization of a property

喜你入骨 提交于 2019-12-21 00:23:13
问题 I want to create a decorator that works like a property, only it calls the decorated function only once, and on subsequent calls always return the result of the first call. An example: def SomeClass(object): @LazilyInitializedProperty def foo(self): print "Now initializing" return 5 >>> x = SomeClass() >>> x.foo Now initializing 5 >>> x.foo 5 My idea was to write a custom decorator for this. So i started, and this is how far I came: class LazilyInitializedProperty(object): def __init__(self,

Python descriptor vs property [duplicate]

坚强是说给别人听的谎言 提交于 2019-12-20 10:24:00
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: When and why might I assign an instance of a descriptor class to a class attribute in Python rather than use a property? I'm confused as to when to use a property vs a descriptor. I read that a property is a specialized descriptor. Can someone please post how this works? 回答1: You should read the docs on what descriptors actually are. The Cliff's Notes version: descriptors are a low-level mechanism that lets you

Maximum recursion depth error with getattr

谁说胖子不能爱 提交于 2019-12-20 04:26:24
问题 I have this code; class NumberDescriptor(object): def __get__(self, instance, owner): name = (hasattr(self, "name") and self.name) if not name: name = [attr for attr in dir(owner) if getattr(owner,attr) is self][0] self.name = name return getattr(instance, '_' + name) def __set__(self,instance, value): name = (hasattr(self, "name") and self.name) if not name: owner = type(instance) name = [attr for attr in dir(owner) if getattr(owner,attr) is self][0] self.name = name setattr(instance, '_' +

behaviour of descriptor concept in python (confusing)

大城市里の小女人 提交于 2019-12-19 10:47:36
问题 I understood python descriptor but I have a little confusion about this.. if you have a class descriptor as follows class Descriptor(object): def __get__(self, instance, owner): print 'getting' return self.value def __set__(self, instance, value): print 'setting' self.value = value def __delete__(self, instance): print 'deleting' del self.value and a class whose attributes we want to manage is something like this.. class Test(object): name = Descriptor() def __init__(self, name): print 'init

Why do managed attributes just work for class attributes and not for instance attributes in python?

拜拜、爱过 提交于 2019-12-19 10:29:32
问题 To illustrate the question check the following code: class MyDescriptor(object): def __get__(self, obj, type=None): print "get", self, obj, type return self._v def __set__(self, obj, value): self._v = value print "set", self, obj, value return None class SomeClass1(object): m = MyDescriptor() class SomeClass2(object): def __init__(self): self.m = MyDescriptor() x1 = SomeClass1() x2 = SomeClass2() x1.m = 1000 # -> set <__main__.MyDescriptor object at 0xb787c7ec> <__main__.SomeClass1 object at

How work pre-defined descriptors in functions?

老子叫甜甜 提交于 2019-12-19 07:47:23
问题 Python functions have a descriptors. I believe that in most cases I shouldn't use this directly but I want to know how works this feature? I tried a couple of manipulations with such an objects: def a(): return 'x' a.__get__.__doc__ 'descr.__get__(obj[, type]) -> value' What is the obj and what is the type? >>> a.__get__() TypeError: expected at least 1 arguments, got 0 >>> a.__get__('s') <bound method ?.a of 's'> >>> a.__get__('s')() TypeError: a() takes no arguments (1 given) Sure that I

Reading comments from .proto files using a Protocol Buffers descriptor object

亡梦爱人 提交于 2019-12-13 13:15:39
问题 I am currently revisiting a project using Google Protocol Buffers. In the project I want to make use of the features Descriptors and Reflection of Protocol Buffers. The official documentation states that the comments of .proto files can be read: With the function DebugStringWithOptions(), called on a message or descriptor. With the function GetSourceLocation(), called on a descriptor. I am unable to retrieve comments, so I think I am doing something completely wrong or that feature isn't

class attribute changing value for no reason

空扰寡人 提交于 2019-12-13 00:49:40
问题 I have a problem with a program I am writing and i cannot for the life of me figure out what I am doing wrong. Ok so basically I am writing a program to extract data from an XML document and manipulate with class representations of the data. Now, I have some added complexity in my program in that I am trying to be smart and use a descriptor (I am learning about them and thought I would try integrating them into my code) Note: I shortened the problem to a self contained python script which can