Python式的? ...动态调用嵌套函数

发布于 2024-12-21 16:35:50 字数 750 浏览 2 评论 0原文

这是一个Pythonic实现吗?

我使用包装器从字符串参数动态调用嵌套函数,以减少调用不存在函数的机会。这是一个示例,我想对 arg1 和 arg2 执行不同的比较(按照 ==、>=、< 等)...

class ComparisonClass(object):
    def__init__(self):
        pass

    def comparison(self,arg1,arg2,comparison):
        def equal_to():               
            pass
        def greater_than():
            pass
        def less_than():
            pass

        return locals()[comparison]()

    def comparison_equal_to(self,arg1,arg2):
        return self.comparison(arg1,arg2,'equal_to')

    def comparison_greater_than(self,arg1,arg2):
        return self.comparison(arg1,arg2,'greater_than')

    def comparison_less_than(self,arg1,arg2):
        return self.comparison(arg1,arg2,'less_than')

Is this a a pythonic implementation?

I'm calling nested functions dynamically from a string argument with wrappers to reduce the chance of calling a non-existent function. Here's an example where I want to perform different comparisons on arg1 and arg2 (in terms of ==, >=, < etc)...

class ComparisonClass(object):
    def__init__(self):
        pass

    def comparison(self,arg1,arg2,comparison):
        def equal_to():               
            pass
        def greater_than():
            pass
        def less_than():
            pass

        return locals()[comparison]()

    def comparison_equal_to(self,arg1,arg2):
        return self.comparison(arg1,arg2,'equal_to')

    def comparison_greater_than(self,arg1,arg2):
        return self.comparison(arg1,arg2,'greater_than')

    def comparison_less_than(self,arg1,arg2):
        return self.comparison(arg1,arg2,'less_than')

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

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

发布评论

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

评论(2

梦断已成空 2024-12-28 16:35:50

实现在这里是无关紧要的,因为您寻求实现的东西并不是 Pythonic 的。

在Python中,我们使用__eq__系列方法进行比较,并且标准库中存在用于根据其他方法实现其中一些方法的工具。

Implementation is irrelevant here because the very thing you seek to implement isn't Pythonic.

In Python, we use the __eq__ family of methods for comparisons, and there exist tools in the standard library for implementing some of them in terms of others.

甚是思念 2024-12-28 16:35:50

你缺少的是,在 Python 中,函数是一等对象。这意味着它们可以被分配给变量并像任何其他对象一样传递。

因此,您不需要嵌套函数并在父函数中调用它,而只需将相关函数指定为函数的返回值,并在返回时调用它。或者更好的是,在类级别分配一个包含函数的字典:

def equal_to():
       pass

   (etc)

COMPARISONS = {
    'equal_to': equal_to,
    etc
}

现在您可以调用 COMPARISONS['equal_to'](arg1, arg2)

What you're missing is that in Python, functions are first-class objects. That means they can be assigned to variables and passed around just like any other object.

So, rather than nesting a function and calling it within the parent function, you just want to assign the relevant function as the return value of your function, and call it on return. Or even better, assign a dictionary at class level containing the functions:

def equal_to():
       pass

   (etc)

COMPARISONS = {
    'equal_to': equal_to,
    etc
}

Now you can call COMPARISONS['equal_to'](arg1, arg2).

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