python 类数据描述符列表
我似乎无法弄清楚如何获取类数据描述符的列表。基本上,我想对字段和未设置的字段进行一些验证。例如:
class Field (object):
def __init__ (self, name, required=False):
self.name = name
self.required = required
def __set__ (self, obj, val):
obj.__dict__[self.name] = val
def __get__ (self, obj):
if obj == None:
raise AttributeError
if self.name not in obj.__dict__:
raise AttributeError
return obj.__dict__[self.name]
然后我想在这样的模型中实现它:
class BaseModel (object):
some_attr = Field('some_attr', required=True)
def save (self):
for field in fields:
if field.required and field.name not in self.__dict__:
raise Exeception, 'Validation Error'
我将如何获取我定义的字段列表?我在想,我可以执行以下操作:
import inspect
fields = []
for attr in self.__class__.__dict__:
if inspect.isdatadescriptor(self.__class__.__dict__[attr]):
fields.append(attr)
但是我遇到了继承问题,有什么想法吗?
I can't seem to figure out how to get a list of a classes data descriptors. Basically, I want to run some validation against the fields and unset fields. For instance:
class Field (object):
def __init__ (self, name, required=False):
self.name = name
self.required = required
def __set__ (self, obj, val):
obj.__dict__[self.name] = val
def __get__ (self, obj):
if obj == None:
raise AttributeError
if self.name not in obj.__dict__:
raise AttributeError
return obj.__dict__[self.name]
Then I wanted to implement it in a model like so:
class BaseModel (object):
some_attr = Field('some_attr', required=True)
def save (self):
for field in fields:
if field.required and field.name not in self.__dict__:
raise Exeception, 'Validation Error'
How would I go about getting a list of the fields I define? I was thinking, I could do the following:
import inspect
fields = []
for attr in self.__class__.__dict__:
if inspect.isdatadescriptor(self.__class__.__dict__[attr]):
fields.append(attr)
But I ran into problems with inheritance, any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了迭代类的成员,您需要使用
inpsect.getmembers
。因此,您的最后一个示例如下所示:在您的
descriptor
中,您应该将直接访问__dict__
替换为 python 的 getattr 和 setattr 内置函数。另外,请记住,您希望区分类的
descriptor
属性名称和底层实例的属性。在这种情况下,我通常会在前面加上下划线。例如:In order to iterate over the classes' members you need to use
inpsect.getmembers
. So your last example would look like this:In your
descriptor
you should replace accessing__dict__
directly with python's getattr and setattr builtins.Also, keep in mind that you want to make a distinction between the
descriptor
attribute's name on the class and the underlying instance's attribute. In cases like this I usually prepend and underscore. e.g.: