我试图动态生成一些类定义(用于包装C ++扩展)。 下面的描述,除了工作,当我尝试访问的文档字符串的使用领域的帮助()的罚款,它提供了默认文档的描述,而不是场它的自我。 然而,当我这样做帮助(类名),它检索传递给描述符的文档字符串:
class FieldDescriptor(object):
def __init__(self, name, doc='No documentation available.'):
self.name = name
self.__doc__ = doc
def __get__(self, obj, dtype=None):
if obj is None and dtype is not None:
print 'Doc is:', self.__doc__
return self
return obj.get_field(self.name)
def __set__(self, obj, value):
obj.set_field(self.name, value)
class TestClass(object):
def __init__(self):
self.fdict = {'a': None, 'b': None}
def get_field(self, name):
return self.fdict[name]
def set_field(self, name, value):
self.fdict[name] = value
fields = ['a', 'b']
def define_class(class_name, baseclass):
class_obj = type(class_name, (baseclass,), {})
for field in fields:
setattr(class_obj, field, FieldDescriptor(field, doc='field %s in class %s' % (field, class_name)))
globals()[class_name] = class_obj
if __name__ == '__main__':
define_class('DerivedClass', TestClass)
help(DerivedClass.a)
help(DerivedClass)
v = DerivedClass()
help(v.a)
“蟒蛇test.py”打印:
Doc is: field a in class DerivedClass Help on FieldDescriptor in module __main__ object: class FieldDescriptor(__builtin__.object) | Methods defined here: | | __get__(self, obj, dtype=None) | | __init__(self, name, doc='No documentation available.') | | __set__(self, obj, value) | | ---------------------------------------------------------------------- | Data descriptors defined here: | | __dict__ | dictionary for instance variables (if defined) | | __weakref__ | list of weak references to the object (if defined) Doc is: field a in class DerivedClass Doc is: field b in class DerivedClass Help on class DerivedClass in module __main__: class DerivedClass(TestClass) | Method resolution order: | DerivedClass | TestClass | __builtin__.object | | Data descriptors defined here: | | a | field a in class DerivedClass | | b | field b in class DerivedClass | | ---------------------------------------------------------------------- | Methods inherited from TestClass: | | __init__(self) | | get_field(self, name) | | set_field(self, name, value) | | ---------------------------------------------------------------------- | Data descriptors inherited from TestClass: | | __dict__ | dictionary for instance variables (if defined) | | __weakref__ | list of weak references to the object (if defined) Help on NoneType object: class NoneType(object) | Methods defined here: | | __hash__(...) | x.__hash__() hash(x) | | __repr__(...) | x.__repr__() repr(x)
任何想法,如何能够获得descriptor.__doc__
的help(class.field)
而且是有办法绕过这一点,有一些像在具有存储在描述符doc字符串代替DOC getter函数?
喜欢:
class FieldDescriptor(object):
def __init__(self, name, doc='No documentation available.'):
self.name = name
self.__doc__ = doc
def __get__(self, obj, dtype=None):
if obj is None and dtype is not None:
print 'Doc is:', self.__doc__
return self
return obj.get_field(self.name)
def __set__(self, obj, value):
obj.set_field(self.name, value)
# This is what I'd like to have
def __doc__(self, obj, dtype):
return dtype.generate_docstring(self.name)
UPDATE:其实我开始用这个定义的__get__
:
def __get__(self, obj, dtype=None):
return obj.get_field(self.name)
这样做的问题是,当我说:
help(DerivedClass.a)
Python中抛出一个异常,表明我是想叫None.get_field
。 因此help()
被调用__get__
与方法obj=None
和dtype=DerivedClass
。 这就是为什么我决定当的obj =无返回FieldDescriptor实例和D型!=无。 我的印象是help(xyz)
试图显示xyz.__doc__
。 由该逻辑,如果__get__
返回descriptor_instance
,然后descriptor_instance.__doc__
应由帮助(),它是整个类[的情况下打印help(DerivedClass)
],但没有为单个字段[ help(DerivedClass.a)
] 。