Python function composition

后端 未结 1 1697
长情又很酷
长情又很酷 2021-02-03 12:17

I\'ve tried to implement function composition with nice syntax and here is what I\'ve got:

from functools import partial

class _compfunc(partial):
    def __lsh         


        
相关标签:
1条回答
  • 2021-02-03 12:56

    append does in-place addition, as Ignacio Vazquez-Abrams said (well, implied) -- so, while you could fix that by just adding a return to your function, it would have the side-effect of changing the argument it was passed, too:

    @composable
    def f4(a):
        a.append(0)
        return a
    

    It would be best to use the following even more concise code which also creates and returns a new object:

    @composable
    def f4(a):
      return a + [0]
    
    0 讨论(0)
提交回复
热议问题