验证子类中的 Python 参数

发布于 2024-07-13 01:21:11 字数 526 浏览 3 评论 0原文

我正在尝试验证一些 python 参数。 在 Python 3.0 中引入新的静态类型之前,解决此问题的最佳方法是什么。

这是我正在尝试的一个示例:

class A(object):
    @accepts(int, int, int)
    def __init__(a, b, c):
        pass

class B(A):
    @accepts(int, int, int, int)
    def __init__(a, b, c, d):
        A.__init__(a, b, c)

正如您所看到的,装饰器很好地对我的类的输入执行类型检查,但我必须定义第二个类的所有参数,当我有多个级别时,这会变得非常讨厌的继承。 我可以使用 kwargs 取得一些成功,但它不如上面的类型检查方法那么好。

本质上,我想从 kwargs 列表中弹出一个参数并检查它的类型,然后将其余部分传递给它的父级,但随着规模的扩展,以一种非常灵活和干净的方式执行此操作。

有什么建议么?

I'm trying to validate a few python arguments. Until we get the new static typing in Python 3.0, what is the best way of going about this.

Here is an example of what I am attempting:

class A(object):
    @accepts(int, int, int)
    def __init__(a, b, c):
        pass

class B(A):
    @accepts(int, int, int, int)
    def __init__(a, b, c, d):
        A.__init__(a, b, c)

As you can see the decorator is nicely performing type checking of the inputs to my class, but I have to define all the arguments to the second class, which gets very nasty when I have multiple levels of inheritance. I can use kwargs with some success, but it's not quite as nice as the above approach for type checking.

Essentially I want to pop one argument off the kwargs list and check it's type, then pass the remainder to it's parent, but do this in a very flexible and clean way as this scales.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

忘你却要生生世世 2024-07-20 01:21:11

为什么不直接定义一个 any 值,并用 @accepts(any,any,any, int) 修饰子类构造函数呢? 您的装饰器不会检查标有 any 的参数,并且超类构造函数上的 @accepts 将检查子类传递给它的所有参数。

Why not just define an any value, and decorate the subclass constructor with @accepts(any, any, any, int)? Your decorator won't check parameters marked with any, and the @accepts on the superclass constructor will check all the arguments passed up to it by subclasses.

戏剧牡丹亭 2024-07-20 01:21:11

您可能想使用 inspect 模块。 它可以让你枚举超类、参数列表和其他有趣的东西。 看来您可能想要检查超类 __init__ 方法的参数列表,并将其与子类中的参数列表进行比较。 我不确定这是否适合你。

不过,我会小心你所做的假设。 假设仅仅因为类 A 有 N 个参数传递给 __init__,子类将至少包含 N 个参数并传递第一个参数,这可能并不安全。 N 一直到超类。 如果子类更具体,那么它可能会填写其超类 __init__ 方法的所有参数。

You might want to play around with the inspect module. It will let you enumerate superclasses, argument lists, and other fun stuff. It seems that you might want to inspect the argument list of the superclass __init__ method and compare it against what you have in the subclass. I'm not sure if this is going to work for you or not.

I would be careful about what assumptions you make though. It might not be safe to assume that just because class A has N arguments to __init__, subclasses will contain at least N arguments and pass the first N through to the super class. If the subclass is more specific, then it might fill in all by 2 of the arguments to its superclasses __init__ method.

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