Python:类似 jQuery 的函数链?
我在 Google 上找不到任何关于这个主题的内容,所以我想我应该在这里问:
Is it possible to chainfunctions with Python, like jQuery does?
['my', 'list'].foo1(arg1, arg2).foo2(arg1, arg2).foo3(arg1, arg2) #etc...
当我编写这段代码时,我失去了很多空间和可读性:
foo3(foo2(foo1(['my', 'list'], arg1, arg2), arg1, arg2), arg1, arg2) #etc...
似乎存在一些用于创建此类函数的虚幻库,但我似乎不明白为什么这必须看起来如此复杂......
谢谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只要函数返回一个值,就可以将其链接起来。在 jQuery 中,选择器方法通常返回选择器本身,这允许您进行链接。如果你想在 python 中实现链接,你可以这样做:
但是,你的问题似乎是你的函数参数太狭窄了。链接不是解决这个问题的方法。如果您想压缩函数参数,只需在将参数传递给函数之前将参数分配给变量,如下所示:
As long as the function returns a value, you can chain it. In jQuery, a selector method usually returns the selector itself, which is what allows you to do the chaining. If you want to implement chaining in python, you could do something like this:
Your problem, however, seems to be that your function arguments are too cramped. Chaining is not a solution to this. If you want to condense your function arguments, just assign the arguments to variables before passing them to the function, like this:
这是 Simon 的
ListMutator
建议的扩展:您可以做得更好,通过使用 ListMutator 完全像一个列表一样工作.org/library/collections.html#abcs-abstract-base-classes" rel="noreferrer">集合抽象基类。事实上,您可以对
list
本身进行子类化,尽管它可能会限制您做某些您可能需要做的事情......而且我不知道对子类化内置类型的普遍看法是什么就像列表
。Here's an expansion of Simon's
ListMutator
suggestion:You could go one better and make
ListMutator
act entirely like a list by using the collections abstract base classes. In fact, you could subclasslist
itself, although it may restrict you from doing certain things you might need to do... and I don't know what the general opinion is on subclassing built-in types likelist
.如果我们谈论对象方法,那么这很简单,只需从每个方法中
返回 self
即可。另一方面,如果您想链接未绑定的函数,那么按照您想要的方式链接它们对我来说并没有什么意义。当然,它看起来不错,但它在语义上不连贯,因为“。”代表对象属性访问而不是“链”。If we're talking about object methods, then it's trivial, just
return self
from every method. On the other hand, if you would like to chain unbound functions, it doesn't really make sense to me to chain them the way you want to. Sure, it looks nice, however it's semantically incoherent because the "." stands for object attribute access and not for "chain".供将来参考:看看 Moka,一个极简主义函数式编程库。从他们的例子来看:
For future reference: have a look at Moka, a minimalist functional programming library. From their examples:
看看这个。它是一个简单的链接包装类。它实现了 underscore.js 库的一些功能。您可以用下划线包装列表、元组或字典,然后使用它,然后通过附加另一个下划线从中获取值。
输出:
Take a look at this. It is A simple wrapper class for chaining. And it implemented some of the underscore.js lib's functionality. You wrap your list, tuple or dict with an underscore, and play with it then get the value out of it by appending another underscore.
output: