10分钟学会python函数式编程
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在这篇<span style="color: black;">文案</span>里,你将学会什么是函数范式以及<span style="color: black;">怎样</span><span style="color: black;">运用</span>Python进行函数式编程。你<span style="color: black;">亦</span>将<span style="color: black;">认识</span>列表推导和其它形式的推导。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">函数范式</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在命令式范式中,<span style="color: black;">经过</span>为计算机<span style="color: black;">供给</span>一系列指令<span style="color: black;">而后</span>执行它们来完成任务。在执行这些指令时,<span style="color: black;">能够</span>改变某些状态。例如,假设你最初将A设置为5,<span style="color: black;">而后</span>更改A的值。<span style="color: black;">此时</span>在变量内部值的<span style="color: black;">道理</span>上,你改变了A的状态。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在函数式范式中,你<span style="color: black;">不消</span>告诉计算机做什么而是告诉他这个东西是什么。<span style="color: black;">例如</span>数字的最大公约数是什么,从1到n的乘积是什么等等。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">因此呢</span>,变量<span style="color: black;">不可</span>变化。一旦你设置了一个变量,它就永远保持这种状态(<span style="color: black;">重视</span>,在纯函数式语言中,它们不是变量)。<span style="color: black;">因此呢</span>,函数式编程<span style="color: black;">无</span>副<span style="color: black;">功效</span>。副<span style="color: black;">功效</span>指的是函数改变它自己以外的东西。让<span style="color: black;">咱们</span>看<span style="color: black;">有些</span>典型Python代码的示例:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/R9dSrBG8ZyXlwT~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723904189&x-signature=wpOGWDX4FD5lG8Uuy2xAQppW1xQ%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这段代码的输出是5。在函数式范式中,改变变量是一个很大的禁忌,并且<span style="color: black;">拥有</span>影响其范围之外事物的功能<span style="color: black;">亦</span>是一个很大的禁忌。函数<span style="color: black;">独一</span>能做的<span style="color: black;">便是</span>计算<span style="color: black;">有些</span>东西并将其<span style="color: black;">做为</span>结果返回。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">此刻</span>你可能会想:“<span style="color: black;">无</span>变量,<span style="color: black;">无</span>副<span style="color: black;">功效</span>?<span style="color: black;">为何</span><span style="color: black;">这般</span>好?“这个问题问得好,我相信大<span style="color: black;">都数</span>人对此感到疑惑。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span><span style="color: black;">运用</span>相同的参数调用函数两次,则<span style="color: black;">保准</span>返回相同的结果。<span style="color: black;">倘若</span>你<span style="color: black;">已然</span>学习了数学函数,你就会<span style="color: black;">晓得</span>这个好处。这<span style="color: black;">叫作</span>为参照透明度。<span style="color: black;">因为</span>函数<span style="color: black;">无</span>副<span style="color: black;">功效</span>,<span style="color: black;">倘若</span>你正在构建一个计算某些事情的程序,你<span style="color: black;">能够</span>加速程序。<span style="color: black;">倘若</span>每次调用func(2)都返回3,<span style="color: black;">咱们</span><span style="color: black;">能够</span>将它存储在表中,这<span style="color: black;">能够</span>防止程序重复运行相同的功能。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">一般</span>,在函数式编程中,<span style="color: black;">咱们</span>不<span style="color: black;">运用</span>循环。<span style="color: black;">咱们</span><span style="color: black;">运用</span>递归。递归是一个数学概念,<span style="color: black;">一般</span><span style="color: black;">寓意</span>着“自我调用”。<span style="color: black;">运用</span>递归函数,该函数将其<span style="color: black;">自己</span><span style="color: black;">做为</span>子函数重复调用。这是Python中递归函数的一个很好的例子:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/R9dSrBPGaLd2PN~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723904189&x-signature=63MNEG%2BNtX2ThdU%2BsQBXaPX77y8%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">有些编程语言<span style="color: black;">亦</span><span style="color: black;">拥有</span>惰性。这<span style="color: black;">寓意</span>着<span style="color: black;">她们</span>直到最后一秒才计算或做任何事情。<span style="color: black;">倘若</span>你编写<span style="color: black;">有些</span>代码来执行2 + 2,函数程序只会在你真正<span style="color: black;">必须</span><span style="color: black;">运用</span>结果时计算出来。<span style="color: black;">咱们</span><span style="color: black;">火速</span>就会在Python中探索惰性。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">Map</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">为了理解,<span style="color: black;">咱们</span>先来<span style="color: black;">瞧瞧</span>迭代是什么。<span style="color: black;">一般</span><span style="color: black;">能够</span>迭代的对象是列表或数组,但Python有许多<span style="color: black;">区别</span>的类型<span style="color: black;">能够</span>迭代。你<span style="color: black;">乃至</span><span style="color: black;">能够</span>创建自己的对象,这些对象<span style="color: black;">能够</span><span style="color: black;">经过</span>实现魔术<span style="color: black;">办法</span>进行迭代。魔术<span style="color: black;">办法</span>就像是一个API,<span style="color: black;">能够</span><span style="color: black;">帮忙</span>你的对象变得更加Pythonic。您<span style="color: black;">必须</span>实现2个魔术<span style="color: black;">办法</span><span style="color: black;">才可</span>使对象<span style="color: black;">作为</span>可迭代的:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/R9dSrBZJGsqiSH~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723904189&x-signature=qeFrZLqGBPeXGPs3hG3hq7%2FlwYQ%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">第1</span>个魔术<span style="color: black;">办法</span>“__iter__”(注:<span style="color: black;">这儿</span>是双下划线)返回迭代对象,这<span style="color: black;">一般</span>在循环<span style="color: black;">起始</span>时<span style="color: black;">运用</span>。”__next__“返回下一个对象。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">让<span style="color: black;">咱们</span>快速进入一个终端调用上面的代码:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/R9dSrCHAOXBlEv~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723904189&x-signature=R5FvI20qQ%2BmhELIQykjx0%2FQS8Ok%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">运行将会打印出</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/R9dSrCR4pLXqkQ~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723904189&x-signature=hlu9nR%2BfOGBoYN4aQVOm7FN1Gd4%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在Python中,迭代器是一个<span style="color: black;">仅有</span>__iter__魔术<span style="color: black;">办法</span>的对象。这<span style="color: black;">寓意</span>着您<span style="color: black;">能够</span><span style="color: black;">拜访</span>对象中的位置,但<span style="color: black;">不可</span>遍历该对象。<span style="color: black;">有些</span>对象将<span style="color: black;">拥有</span>魔术<span style="color: black;">办法</span>__next__而不是__iter__魔术<span style="color: black;">办法</span>,例如集合(在本文后面讨论)。<span style="color: black;">针对</span>本文,<span style="color: black;">咱们</span>假设<span style="color: black;">咱们</span>接触的所有内容都是可迭代的对象。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">此刻</span><span style="color: black;">咱们</span><span style="color: black;">晓得</span>什么是可迭代对象了,让<span style="color: black;">咱们</span>回到map函数。 map函数<span style="color: black;">准许</span><span style="color: black;">咱们</span>将函数应用于iterable中的每一项。 Map<span style="color: black;">必须</span>2个输入,它们分别是要应用的函数和可迭代对象。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/R9dSrRI3QqOi9P~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723904189&x-signature=bUDP%2Bl5DsDBIBY%2FiFHOUoLRyOgE%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">假设<span style="color: black;">咱们</span>有一个数字列表,如下所示:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/R9dSrRTIiyt0Oz~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723904189&x-signature=XvqDe3U%2FADG2QJKP0UPjPaWRbEg%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>想要对<span style="color: black;">每一个</span>数字进行平方,<span style="color: black;">咱们</span><span style="color: black;">能够</span>编写如下代码:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/R9dSrRcEcJSrl0~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723904189&x-signature=TrGHxE6%2BO3Is0hqjm0HR7vqWUzA%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Python中函数式的函数是<span style="color: black;">拥有</span>惰性的。<span style="color: black;">倘若</span><span style="color: black;">咱们</span>不<span style="color: black;">运用</span>“list”,该函数将存储iterable的定义,而不是列表本身。<span style="color: black;">咱们</span><span style="color: black;">必须</span><span style="color: black;">知道</span>告诉Python“把它变成一个列表”供<span style="color: black;">咱们</span><span style="color: black;">运用</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在Python中<span style="color: black;">忽然</span>从非惰性求值转向惰性求值有点奇怪。<span style="color: black;">倘若</span>你在函数式思维方式中<span style="color: black;">思虑</span>得<span style="color: black;">更加多</span>,而不是命令式思维方式,<span style="color: black;">那样</span>你<span style="color: black;">最后</span>会习惯它。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">此刻</span>写一个像“square(num)”<span style="color: black;">这般</span>的普通函数虽然很好,但却是不对的。<span style="color: black;">咱们</span><span style="color: black;">必要</span>定义一个完整的函数<span style="color: black;">才可</span>在map中<span style="color: black;">运用</span>它?好吧,<span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">运用</span>lambda(匿名)函数在map中定义一个函数。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">Lambda表达式</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">lambda表达式是一个<span style="color: black;">仅有</span>一行的函数。举个例子,这个lambda表达式对给定的数字进行平方:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/R9dSrRn2vDHEp~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723904189&x-signature=Cb8a07dnuiGYQ%2F9Iq5gfpT7mQFM%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">让<span style="color: black;">咱们</span>运行它:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/R9dSrRyAsIkAl~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723904189&x-signature=I0JYBBZziXFUhtwDIHCvPdV7Ijc%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这看起来不像一个函数吗?</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">嗯,这有点令人困惑,但<span style="color: black;">能够</span>解释。<span style="color: black;">咱们</span>将<span style="color: black;">有些</span>东西分配给变量“square”。那这个呢:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/R9dSrVf59mLkaL~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723904189&x-signature=nPUjkJS2RTFteTJi7lDFPHzCWVI%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">告诉Python这是一个lambda函数,输入叫做x。冒号之后的任何内容都是您对输入所做的操作,它会自动返回结果。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">简化<span style="color: black;">咱们</span>的square程序到<span style="color: black;">仅有</span>一行代码,<span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">这般</span>做:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/R9dSrWHCRSM6Np~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723904189&x-signature=2d%2FyY0h9EMpghPPlQjqXP9cyG9o%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">因此</span>在lambda表达式中,所有参数都在左边,你要用它们做的东西在右边。它有点乱。但事实是,编写<span style="color: black;">仅有</span>其他函数式程序员<span style="color: black;">才可</span>阅读的代码会有<span style="color: black;">必定</span>的乐趣。<span style="color: black;">另外</span>,<span style="color: black;">运用</span>一个函数并将其转换为一行代码是非常酷的。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">Reduce</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Reduce是一个将迭代变成一个东西的函数。<span style="color: black;">一般</span>,你<span style="color: black;">能够</span>在列表上<span style="color: black;">运用</span>reduce函数执行计算以将其减少到一个数字。 Reduce看起来像<span style="color: black;">这般</span>:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/R9dSrWR438sGav~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723904189&x-signature=jIuwuxE2xHvZBzC1TCunLx8TnUo%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>经常会<span style="color: black;">运用</span>lambda表达式<span style="color: black;">做为</span>函数。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">列表的乘积是<span style="color: black;">每一个</span>单独的数字相乘。要做到这一点你将编写如下代码:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/R9dSrWbDAP8Epc~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723904189&x-signature=ny3Yjd6%2BPuGTrEmKt0oUMzDByvQ%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">然则</span><span style="color: black;">运用</span>reduce你<span style="color: black;">能够</span><span style="color: black;">这般</span>写:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/R9dSrWrAXeQ2He~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723904189&x-signature=hxGwfwUFy%2Fs9oYH4XrCf8zx7Tek%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">得到</span>相同的功能,代码更短,并且在<span style="color: black;">运用</span>函数式编程的<span style="color: black;">状况</span>下更整洁。(注:reduce函数在Python3中已不是内置函数,<span style="color: black;">必须</span>从functools模块中导入)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">Filter</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">filter函数采用可迭代的方式,并过滤掉你在该可迭代中不<span style="color: black;">必须</span>的所有内容。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">一般</span>,filter<span style="color: black;">必须</span>一个函数和一个列表。它将函数应用于列表中的每一项,<span style="color: black;">倘若</span>该函数返回True,则不执行任何操作。<span style="color: black;">倘若</span>返回False,则从列表中删除该项。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">语法如下:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/R9dSriH9rvArv3~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723904189&x-signature=bgr2X0mqubc7N8G%2FMykROciOLLM%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">让<span style="color: black;">咱们</span>看一个小例子,<span style="color: black;">无</span>filter<span style="color: black;">咱们</span>会写:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/R9dSriV7JfkjlT~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723904189&x-signature=5UgT9wQo2AP%2FEZalymuiY1tOclE%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运用</span>filter,<span style="color: black;">能够</span><span style="color: black;">这般</span>写:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/R9dSrig1mpQHQb~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723904189&x-signature=Ean7AM1I83TSEcUdIrzJ8sFlt4o%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">高阶函数</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">高阶函数<span style="color: black;">能够</span>将函数<span style="color: black;">做为</span>参数并返回函数。一个非常简单的例子如下:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/R9dSriqIB8fDUT~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723904189&x-signature=fA7iEeoZPtgcjrvqAm8%2Fgm%2Bm0uk%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">第二个返回函数的例子:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/R9dSrj16fwjJYG~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723904189&x-signature=CaDd2BPxfPX8sel96DeXl3weuvE%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">开头我说过纯函数式编程语言<span style="color: black;">无</span>变量。更高阶的函数使这变得<span style="color: black;">更易</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Python中的所有函数都是一等<span style="color: black;">百姓</span>。一等<span style="color: black;">百姓</span>被定义为<span style="color: black;">拥有</span>以下一个或多个特征:</p>在运行时创建在数据结构中分配变量或元素<span style="color: black;">做为</span>函数的参数传递<span style="color: black;">做为</span>函数的结果返回Python中的所有函数都<span style="color: black;">能够</span>用作高阶函数。<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">Partial application</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Partial application(<span style="color: black;">亦</span><span style="color: black;">叫作</span>为闭包)有点奇怪,但非常酷。您<span style="color: black;">能够</span>在不<span style="color: black;">供给</span>所需的所有参数的<span style="color: black;">状况</span>下调用函数。让<span style="color: black;">咱们</span>在一个例子中看到这一点。<span style="color: black;">咱们</span>想要创建一个函数,它接受2个参数,一个基数和一个指数,并返回指数幂的基数,如下所示:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/R9dSrpe7SSj2Ep~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723904189&x-signature=xV9t1Fw%2FuJwDvBQ%2B9Nv260Ye8e8%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">此刻</span><span style="color: black;">咱们</span>想要一个专用的平方函数,<span style="color: black;">运用</span>幂函数计算出数字的平方:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/R9dSrpvGO5EmCA~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723904189&x-signature=CHcOBE1Qzcf%2BxhU6pvNZdnhtUdA%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这有效,但<span style="color: black;">倘若</span><span style="color: black;">咱们</span>想要一个立方体功能呢?<span style="color: black;">或</span>求四次方的功能呢?<span style="color: black;">咱们</span><span style="color: black;">能够</span>继续写下它们吗?好吧,你<span style="color: black;">能够</span>。但程序员很懒的。<span style="color: black;">倘若</span>你一遍又一遍地重复<span style="color: black;">一样</span>的事情,这<span style="color: black;">显示</span>有一种更快的<span style="color: black;">办法</span>来加快速度,这将使你<span style="color: black;">再也不</span>重复。<span style="color: black;">咱们</span><span style="color: black;">能够</span>在<span style="color: black;">这儿</span><span style="color: black;">运用</span>闭包。让<span style="color: black;">咱们</span>看一个<span style="color: black;">运用</span>闭包的square函数的示例:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/R9dSrqGG6hUxMk~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723904189&x-signature=fDUcCdpqkKayMDO2NF35ahsSwWA%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">是不是很酷!<span style="color: black;">咱们</span><span style="color: black;">能够</span>只<span style="color: black;">运用</span>1个参数来调用<span style="color: black;">必须</span>2个参数的函数。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>还<span style="color: black;">能够</span><span style="color: black;">运用</span>一个循环来生成一个幂函数,该函数实现从立方体<span style="color: black;">始终</span>到1000的幂。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/R9dSrqP2CdqnYi~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723904189&x-signature=kFB77Rjk2RRVpMDQOvx7%2FT%2FnaxM%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">函数式编程不是pythonic</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">您可能<span style="color: black;">已然</span><span style="color: black;">重视</span>到了,<span style="color: black;">咱们</span>想要在函数式编程中做的<span style="color: black;">非常多</span>事情都围绕着列表。除了reduce函数和闭包之外,您看到的所有函数都会生成列表。 Guido(Python之父)不<span style="color: black;">爱好</span>Python中的函数式,<span style="color: black;">由于</span>Python<span style="color: black;">已然</span>有了自己生成列表的<span style="color: black;">办法</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>你在Python的交互环境下写入”import this“,你将会得到:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/R9dSrqZ9iiPzMO~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723904189&x-signature=KC3fYW%2B2DE8PsZOYWXnd73Z2WIQ%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这是Python之禅。这是一首关于Pythonic<span style="color: black;">寓意</span>着什么的诗。<span style="color: black;">咱们</span>想要<span style="color: black;">触及</span>的部分是:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">There should be one — and preferably only one — obvious way to do it.(应该<span style="color: black;">尽可能</span>找到一种,最好是<span style="color: black;">独一</span>一种<span style="color: black;">显著</span>的<span style="color: black;">处理</span><span style="color: black;">方法</span>)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在Python中,map和filter<span style="color: black;">能够</span>执行与列表推导(下面讨论)相同的操作。这打破了Python之禅的一个规则,<span style="color: black;">因此呢</span>函数式编程的这些部分不被视为“pythonic”。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">另一个<span style="color: black;">专题</span>是Lambda。在Python中,lambda函数是一个普通函数。 Lambda是语法糖。这两种说法是等价的。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/R9dSs0S1lEMF3u~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723904189&x-signature=IOdqlvxEfXILCEs%2F5jRE4Emvulo%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">普通函数<span style="color: black;">能够</span>执行lambda函数<span style="color: black;">能够</span>执行的所有操作,但它<span style="color: black;">不可</span>以相反的方式工作。 lambda函数<span style="color: black;">不可</span>完成普通函数<span style="color: black;">能够</span>执行的所有操作。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这是一个简短的论证,<span style="color: black;">为何</span>函数式编程<span style="color: black;">不可</span>很好地适应<span style="color: black;">全部</span>Python生态系统。你可能<span style="color: black;">已然</span><span style="color: black;">重视</span>到我之前<span style="color: black;">说到</span>了列表推导,<span style="color: black;">咱们</span><span style="color: black;">此刻</span>将讨论它们。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">列表推导</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">前面,我<span style="color: black;">说到</span>过你<span style="color: black;">能够</span>用map或filter做的任何事情,你<span style="color: black;">能够</span>用列表推导。列表推导是一种在Python中生成列表的<span style="color: black;">办法</span>。语法是:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/R9dSs0cBwLUZwW~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723904189&x-signature=SukIVgOWWCdgInFWQZacL3GB7Is%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">让<span style="color: black;">咱们</span>对列表中的<span style="color: black;">每一个</span>数字进行平方,例如:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/R9dSs0o6heYAfm~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723904189&x-signature=RKX93GzsWMVQTmdKDtl5BkgZ%2BTw%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span><span style="color: black;">能够</span>看到<span style="color: black;">怎样</span>将函数应用于列表中的每一项。<span style="color: black;">咱们</span><span style="color: black;">怎样</span>应用filter呢?<span style="color: black;">瞧瞧</span>前面的代码:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/R9dSs0xCrrXG5r~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723904189&x-signature=P7yhql9nFMw76M4buUcs72Jwj7w%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span><span style="color: black;">能够</span>将其转换成一个列表推导,像<span style="color: black;">这般</span>:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/R9dSs1OC4YKdA3~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723904189&x-signature=N%2FcEjpgeZ9elEPCjsniIvoR%2Flb4%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">列表支持if<span style="color: black;">这般</span>的语句。您<span style="color: black;">再也不</span><span style="color: black;">必须</span>将一百万个函数应用于某些东西以<span style="color: black;">得到</span>您想要的东西。事实上,<span style="color: black;">倘若</span>你想尝试生成某种列表,<span style="color: black;">那样</span><span style="color: black;">运用</span>列表推导看起来会更清晰,<span style="color: black;">更易</span>。<span style="color: black;">倘若</span><span style="color: black;">咱们</span>想要将列表中<span style="color: black;">每一个</span>0以下的数字平方怎么办?有了lambda,map和filter你会写:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/R9dSs7X60MoiTG~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723904189&x-signature=3gLUSyAoesc1J%2Fry4mEeT7vcyf4%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这似乎很长很<span style="color: black;">繁杂</span>。<span style="color: black;">经过</span>列表推导,它只是:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/R9dSs7jJ0Nh4vj~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723904189&x-signature=DHLub4vE1Hs8uXQZdMcmsXFaETY%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">列表推导仅适用于列表。map,filter适合任何可迭代的对象,<span style="color: black;">那样</span>这有什么用呢?你<span style="color: black;">能够</span>对你遇到的任何可迭代对象<span style="color: black;">运用</span>任何推导。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">其他推导</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">你<span style="color: black;">能够</span>为任何可迭代对象创建一个推导。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">能够</span><span style="color: black;">运用</span>推导生成任何可迭代的对象。从Python 2.7<span style="color: black;">起始</span>,您<span style="color: black;">乃至</span><span style="color: black;">能够</span>生成字典(hashmap)。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>它是可迭代的,则<span style="color: black;">能够</span>生成它。让<span style="color: black;">咱们</span>看一下最后一组的例子。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/R9dSs7wBXQMti9~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723904189&x-signature=n4ARM2E1LGESSigUo9aKaN7XA4o%3D" style="width: 50%; margin-bottom: 20px;"></div>set是一个元素列表,在该列表中<span style="color: black;">无</span>元素重复两次。set中的元素<span style="color: black;">无</span><span style="color: black;">次序</span>。<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/R9dSs855aljiPB~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723904189&x-signature=RPYz9QZtcq7zQd5TjtVGq6%2BylzY%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">您可能会<span style="color: black;">重视</span>到set(集合)与dict(字典)<span style="color: black;">拥有</span>相同的花括号。 Python非常聪明。<span style="color: black;">按照</span>你是否为dict<span style="color: black;">供给</span>值,它会<span style="color: black;">晓得</span>你是在写dict推导还是set推导。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">总结</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">函数式编程美观而纯粹。函数式代码<span style="color: black;">能够</span>很干净,但<span style="color: black;">亦</span>可能很乱。<span style="color: black;">有些</span>Python程序员不<span style="color: black;">爱好</span>Python中的函数式编程。但我认为,你应该在<span style="color: black;">处理</span>问题时,<span style="color: black;">运用</span>最佳工具。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">英文原文:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">https://qiniumedia.freelycode.com/vcdn/1/<span style="color: black;">优秀</span><span style="color: black;">文案</span>长图</p>2/learnfunctionprogramin10minutes.pdf
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">译者:xiaocai</p>
外贸论坛是我们的,责任是我们的,荣誉是我们的,成就是我们的,辉煌是我们的。
页:
[1]