>>>定义添加(v):...返回 CustomInt(v)>>>添加(1)1>>>添加(1)(2)3>>>add(1)(2)(3)(44) # 等等..50另外,作为一个int子类,返回值保留了int的__repr__和__str__行为s.对于更复杂的操作,您应该适当地定义其他错误.正如@Caridorc 在评论中指出的,add 也可以简单地写成:add = CustomInt将类重命名为 add 而不是 CustomInt 也类似.定义一个闭包,需要额外调用 yield value:我能想到的唯一另一种方法涉及嵌套函数,该函数需要额外的空参数调用才能返回结果.我不使用 nonlocal 并选择将属性附加到函数对象以使其在 Python 之间可移植:def add(v):def_inner_adder(val=None):"""如果 val 是 None 我们返回 _inner_adder.v否则我们增加并返回自己"""如果 val 为 None:返回_inner_adder.v_inner_adder.v += val返回_inner_adder_inner_adder.v = v # 保存值返回_inner_adder这会不断返回自身 (_inner_adder),如果提供了 val,则增加它 (_inner_adder += val),如果没有, 按原样返回值.就像我提到的,它需要一个额外的 () 调用来返回递增的值:>>>添加(1)(2)()3>>>add(1)(2)(3)() # 等等..6On Codewars.com I encountered the following task:While I'm familiar with the basics of Python, I've never encountered a function that is able to be called in such succession, i.e. a function f(x) that can be called as f(x)(y)(z).... Thus far, I'm not even sure how to interpret this notation.As a mathematician, I'd suspect that f(x)(y) is a function that assigns to every x a function g_{x} and then returns g_{x}(y) and likewise for f(x)(y)(z).Should this interpretation be correct, Python would allow me to dynamically create functions which seems very interesting to me. I've searched the web for the past hour, but wasn't able to find a lead in the right direction. Since I don't know how this programming concept is called, however, this may not be too surprising.How do you call this concept and where can I read more about it? 解决方案 I don't know whether this is function chaining as much as it's callable chaining, but, since functions are callables I guess there's no harm done. Either way, there's two ways I can think of doing this:Sub-classing int and defining __call__:The first way would be with a custom int subclass that defines __call__ which returns a new instance of itself with the updated value:class CustomInt(int): def __call__(self, v): return CustomInt(self + v)Function add can now be defined to return a CustomInt instance, which, as a callable that returns an updated value of itself, can be called in succession:>>> def add(v):... return CustomInt(v)>>> add(1)1>>> add(1)(2)3>>> add(1)(2)(3)(44) # and so on..50In addition, as an int subclass, the returned value retains the __repr__ and __str__ behavior of ints. For more complex operations though, you should define other dunders appropriately.As @Caridorc noted in a comment, add could also be simply written as:add = CustomIntRenaming the class to add instead of CustomInt also works similarly.Define a closure, requires extra call to yield value:The only other way I can think of involves a nested function that requires an extra empty argument call in order to return the result. I'm not using nonlocal and opt for attaching attributes to the function objects to make it portable between Pythons:def add(v): def _inner_adder(val=None): """ if val is None we return _inner_adder.v else we increment and return ourselves """ if val is None: return _inner_adder.v _inner_adder.v += val return _inner_adder _inner_adder.v = v # save value return _inner_adderThis continuously returns itself (_inner_adder) which, if a val is supplied, increments it (_inner_adder += val) and if not, returns the value as it is. Like I mentioned, it requires an extra () call in order to return the incremented value:>>> add(1)(2)()3>>> add(1)(2)(3)() # and so on..6 这篇关于Python 中的函数链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-16 07:52