python 类数据描述符列表

发布于 2024-12-08 16:29:31 字数 1026 浏览 1 评论 0原文

我似乎无法弄清楚如何获取类数据描述符的列表。基本上,我想对字段和未设置的字段进行一些验证。例如:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

☆獨立☆ 2024-12-15 16:29:31

为了迭代类的成员,您需要使用inpsect.getmembers。因此,您的最后一个示例如下所示:

import inspect

fields = []
for member_name, member_object in inspect.getmembers(self.__class__):
    if inspect.isdatadescriptor(member_object):
         fields.append(member_name)

在您的 descriptor 中,您应该将直接访问 __dict__ 替换为 python 的 getattrsetattr 内置函数。

另外,请记住,您希望区分类的 descriptor 属性名称和底层实例的属性。在这种情况下,我通常会在前面加上下划线。例如:

class Field (object):
    def __init__(self, name, required=False):
        self.name = '_' + name
        self.required = required 

    def __set__(self, obj, val):
        setattr(obj, self.name, val)

    def __get__(self, obj):
        return getattr(obj, self.name)

In order to iterate over the classes' members you need to use inpsect.getmembers. So your last example would look like this:

import inspect

fields = []
for member_name, member_object in inspect.getmembers(self.__class__):
    if inspect.isdatadescriptor(member_object):
         fields.append(member_name)

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.:

class Field (object):
    def __init__(self, name, required=False):
        self.name = '_' + name
        self.required = required 

    def __set__(self, obj, val):
        setattr(obj, self.name, val)

    def __get__(self, obj):
        return getattr(obj, self.name)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文