• <bdo id='2bv19'></bdo><ul id='2bv19'></ul>
  • <legend id='2bv19'><style id='2bv19'><dir id='2bv19'><q id='2bv19'></q></dir></style></legend>
    <i id='2bv19'><tr id='2bv19'><dt id='2bv19'><q id='2bv19'><span id='2bv19'><b id='2bv19'><form id='2bv19'><ins id='2bv19'></ins><ul id='2bv19'></ul><sub id='2bv19'></sub></form><legend id='2bv19'></legend><bdo id='2bv19'><pre id='2bv19'><center id='2bv19'></center></pre></bdo></b><th id='2bv19'></th></span></q></dt></tr></i><div id='2bv19'><tfoot id='2bv19'></tfoot><dl id='2bv19'><fieldset id='2bv19'></fieldset></dl></div>
    <tfoot id='2bv19'></tfoot>

    <small id='2bv19'></small><noframes id='2bv19'>

        Python中的函数链

        Function chaining in Python(Python中的函数链)

              <tbody id='BTZzz'></tbody>
            • <bdo id='BTZzz'></bdo><ul id='BTZzz'></ul>
              <i id='BTZzz'><tr id='BTZzz'><dt id='BTZzz'><q id='BTZzz'><span id='BTZzz'><b id='BTZzz'><form id='BTZzz'><ins id='BTZzz'></ins><ul id='BTZzz'></ul><sub id='BTZzz'></sub></form><legend id='BTZzz'></legend><bdo id='BTZzz'><pre id='BTZzz'><center id='BTZzz'></center></pre></bdo></b><th id='BTZzz'></th></span></q></dt></tr></i><div id='BTZzz'><tfoot id='BTZzz'></tfoot><dl id='BTZzz'><fieldset id='BTZzz'></fieldset></dl></div>
                • <tfoot id='BTZzz'></tfoot>
                • <small id='BTZzz'></small><noframes id='BTZzz'>

                • <legend id='BTZzz'><style id='BTZzz'><dir id='BTZzz'><q id='BTZzz'></q></dir></style></legend>

                  本文介绍了Python中的函数链的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 Codewars.com 我遇到了以下任务:

                  On Codewars.com I encountered the following task:

                  创建一个函数add,在连续调用时将数字相加.所以 add(1) 应该返回 1add(1)(2) 应该返回 1+2,...

                  Create a function add that adds numbers together when called in succession. So add(1) should return 1, add(1)(2) should return 1+2, ...

                  虽然我熟悉 Python 的基础知识,但我从未遇到过能够连续调用的函数,即可以调用的函数 f(x)f(x)(y)(z)....到目前为止,我什至不确定如何解释这个符号.

                  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.

                  作为一名数学家,我怀疑 f(x)(y) 是一个函数,它为每个 x 分配一个函数 g_{x} 然后返回 g_{x}(y)f(x)(y)(z).

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

                  如果这种解释是正确的,Python 将允许我动态创建对我来说非常有趣的函数.在过去的一个小时里,我在网上搜索了一个小时,但无法找到正确方向的线索.不过,由于我不知道这个编程概念是如何被调用的,所以这可能不会太令人惊讶.

                  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?

                  推荐答案

                  我不知道这是 function 链接和 callable 链接一样多,但是,因为函数 可调用的,我想这并没有什么坏处.无论哪种方式,我都可以想到两种方法:

                  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:

                  第一种方法是使用定义 int 子类="noreferrer">__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)
                  

                  add 函数现在可以定义为返回一个 CustomInt 实例,该实例作为返回自身更新值的可调用对象,可以连续调用:

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

                  另外,作为一个int子类,返回值保留了int__repr____str__行为s.但对于更复杂的操作,您应该适当地定义其他 dunders.

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

                  正如@Caridorc 在评论中指出的那样,add 也可以简单地写成:

                  As @Caridorc noted in a comment, add could also be simply written as:

                  add = CustomInt 
                  

                  将类重命名为 add 而不是 CustomInt 也可以类似地工作.

                  Renaming the class to add instead of CustomInt also works similarly.

                  我能想到的唯一其他方法涉及一个嵌套函数,该函数需要一个额外的空参数调用才能返回结果.我使用 nonlocal 并选择将属性附加到函数对象以使其在 Python 之间可移植:

                  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_adder 
                  

                  这会不断返回自身 (_inner_adder),如果提供了 val,则将其递增 (_inner_adder += val),否则, 按原样返回值.就像我提到的,它需要一个额外的 () 调用才能返回递增的值:

                  This 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中的函数链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)
                    <bdo id='xyoW6'></bdo><ul id='xyoW6'></ul>
                  • <tfoot id='xyoW6'></tfoot>

                    <legend id='xyoW6'><style id='xyoW6'><dir id='xyoW6'><q id='xyoW6'></q></dir></style></legend>

                    <small id='xyoW6'></small><noframes id='xyoW6'>

                      <i id='xyoW6'><tr id='xyoW6'><dt id='xyoW6'><q id='xyoW6'><span id='xyoW6'><b id='xyoW6'><form id='xyoW6'><ins id='xyoW6'></ins><ul id='xyoW6'></ul><sub id='xyoW6'></sub></form><legend id='xyoW6'></legend><bdo id='xyoW6'><pre id='xyoW6'><center id='xyoW6'></center></pre></bdo></b><th id='xyoW6'></th></span></q></dt></tr></i><div id='xyoW6'><tfoot id='xyoW6'></tfoot><dl id='xyoW6'><fieldset id='xyoW6'></fieldset></dl></div>

                            <tbody id='xyoW6'></tbody>