Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1741965
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: Python/Ruby

2018-08-23 09:34:14

这个实现的代码在这里:

代码我在这里也照样给抄过来了

点击(此处)折叠或打开

  1. def pype(x, *fs):
  2.     """
  3.     Pipe function. Takes an initial value and any number of functions/methods.
  4.     Methods as strings. Additional args are supported for functions & methods
  5.     by suppling a step as a tuple/list with function/method as the first
  6.     element and the args as the rest. The pipe input is used as the last
  7.     argument in this case. Currently no kwargs.
  8.     """
  9.     while fs:
  10.         f = fs[0]
  11.         args = []
  12.         if isinstance(f, (list, tuple)):
  13.             args = list(f[1:])
  14.             f = f[0]
  15.         if isinstance(f, str):
  16.             if f.startswith('.'):
  17.                 x = getattr(x, f[1:])(*args)
  18.             else:
  19.                 x = x[f]
  20.         elif isinstance(f, int):
  21.             x = x[f]
  22.         else:
  23.             x = f(*args + [x])
  24.         fs = fs[1:]
  25.     return x

我比较感兴趣的是这一句代码:
x = f(*args + [x]), 所以我将这个代码给修改了下,
x = f(*args, x), 得到的结果完全一样。不知道为什么他会那么写。搞不懂。
如果写成 x = f(*args, [x]) ,得到的结果也很有趣。 
下面就是比较: 

点击(此处)折叠或打开

  1. def pype_2(x, *fs):
  2.     """
  3.     Pipe function. Takes an initial value and any number of functions/methods.
  4.     Methods as strings. Additional args are supported for functions & methods
  5.     by suppling a step as a tuple/list with function/method as the first
  6.     element and the args as the rest. The pipe input is used as the last
  7.     argument in this case. Currently no kwargs.
  8.     """
  9.     while fs:
  10.         f = fs[0]
  11.         args = []
  12.         if isinstance(f, (list, tuple)):
  13.             args = list(f[1:])
  14.             f = f[0]
  15.         if isinstance(f, str):
  16.             if f.startswith('.'):
  17.                 x = getattr(x, f[1:])(*args)
  18.             else:
  19.                 x = x[f]
  20.         elif isinstance(f, int):
  21.             x = x[f]
  22.         else:
  23.             #difference lies here
  24.             x = f(*args, x)
  25.         fs = fs[1:]
  26.     return x


  27. def pype_3(x, *fs):
  28.     """
  29.     Pipe function. Takes an initial value and any number of functions/methods.
  30.     Methods as strings. Additional args are supported for functions & methods
  31.     by suppling a step as a tuple/list with function/method as the first
  32.     element and the args as the rest. The pipe input is used as the last
  33.     argument in this case. Currently no kwargs.
  34.     """
  35.     while fs:
  36.         f = fs[0]
  37.         args = []
  38.         if isinstance(f, (list, tuple)):
  39.             args = list(f[1:])
  40.             f = f[0]
  41.         if isinstance(f, str):
  42.             if f.startswith('.'):
  43.                 x = getattr(x, f[1:])(*args)
  44.             else:
  45.                 x = x[f]
  46.         elif isinstance(f, int):
  47.             x = x[f]
  48.         else:
  49.             #difference lies here
  50.             x = f(*args, [x])
  51.         fs = fs[1:]
  52.     return x

  53. def add_suffix(number, s):
  54.     return '{} is {} cool!'.format(
  55.         s,
  56.         ' '.join('very' for _ in range(number))
  57.     )

  58. print(pype(
  59.     ' abc: {} ',
  60.     '.strip',
  61.     ('.format', 3),
  62.     (add_suffix, 2),
  63.     '.upper',
  64. ))

  65. print(pype_2(
  66.     ' abc: {} ',
  67.     '.strip',
  68.     ('.format', 3),
  69.     (add_suffix, 2),
  70.     '.upper',
  71. ))

  72. print(pype_3(
  73.     ' abc: {} ',
  74.     '.strip',
  75.     ('.format', 3),
  76.     (add_suffix, 2),
  77.     '.upper',
  78. ))
结果如下:非常有意思

点击(此处)折叠或打开


  1. In [6]: %run -i /usr/local/src/py/pype.py

    ABC: 3 IS VERY VERY COOL!

    ABC: 3 IS VERY VERY COOL!

    ['ABC: 3'] IS VERY VERY COOL!






阅读(984) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~