美文网首页科研钥匙
鲜为人知的 Python 语法

鲜为人知的 Python 语法

作者: 轻松学Python111 | 来源:发表于2019-03-12 13:45 被阅读420次
    image.png

    所有人(好吧,不是所有人)都知道 Python 是一门用途广泛、易读、而且容易入门的编程语言。

    但同时 Python 语法也允许我们做一些很奇怪的事情。

    使用 lambda 表达式重写多行函数

    众所周知 python 的 lambda 表达式不支持多行代码。但是可以模拟出多行代码的效果。

    <pre class=" language-python" style="border: 1px solid rgba(0, 0, 0, 0.0980392); font-size: 15px; font-style: normal; font-weight: normal; margin: 1em 2em; outline: 0px; padding: 1em; vertical-align: baseline; font-family: Consolas, Monaco, "andale mono", "ubuntu mono", monospace; background: rgb(245, 242, 240); color: rgb(0, 0, 0); hyphens: none; line-height: 1.5; box-sizing: border-box; max-width: 100%; overflow: auto; white-space: pre; word-wrap: normal; max-height: 500px; text-shadow: rgb(255, 255, 255) 0px 1px; text-align: left; word-spacing: 0px; word-break: normal; tab-size: 4; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: 0.5px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px;">def f():
    x = 'string'
    if x.endswith('g'):
    x = x[:-1]
    r = ''
    for i in xrange(len(x)):
    if x[i] != 'i':
    r += x[i]
    return r
    f()
    -> 'strn'
    </pre>

    虽然看起来很奇怪,但是上面的函数可以使用下面的 lambda 表达式函数代替:

    <pre class=" language-python" style="border: 1px solid rgba(0, 0, 0, 0.0980392); font-size: 15px; font-style: normal; font-weight: normal; margin: 1em 2em; outline: 0px; padding: 1em; vertical-align: baseline; font-family: Consolas, Monaco, "andale mono", "ubuntu mono", monospace; background: rgb(245, 242, 240); color: rgb(0, 0, 0); hyphens: none; line-height: 1.5; box-sizing: border-box; max-width: 100%; overflow: auto; white-space: pre; word-wrap: normal; max-height: 500px; text-shadow: rgb(255, 255, 255) 0px 1px; text-align: left; word-spacing: 0px; word-break: normal; tab-size: 4; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: 0.5px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px;">(lambda: ([x for x in ['string']], x.endswith('g') and [x for x in [x[:-1]]], [r for r in ['']], [x[i] != 'i' and [r for r in [r+x[i]]] for i in xrange(len(x))], r)[-1])()
    -> 'strn'
    </pre>

    永远不要在生产环境写这样的代码 🙂

    三元运算符

    现代的 python 提供了更简便的语法:

    <pre class=" language-python" style="border: 1px solid rgba(0, 0, 0, 0.0980392); font-size: 15px; font-style: normal; font-weight: normal; margin: 1em 2em; outline: 0px; padding: 1em; vertical-align: baseline; font-family: Consolas, Monaco, "andale mono", "ubuntu mono", monospace; background: rgb(245, 242, 240); color: rgb(0, 0, 0); hyphens: none; line-height: 1.5; box-sizing: border-box; max-width: 100%; overflow: auto; white-space: pre; word-wrap: normal; max-height: 500px; text-shadow: rgb(255, 255, 255) 0px 1px; text-align: left; word-spacing: 0px; word-break: normal; tab-size: 4; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: 0.5px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px;">b if a else c
    </pre>

    也可以通过下面的方式重写:

    <pre class=" language-python" style="border: 1px solid rgba(0, 0, 0, 0.0980392); font-size: 15px; font-style: normal; font-weight: normal; margin: 1em 2em; outline: 0px; padding: 1em; vertical-align: baseline; font-family: Consolas, Monaco, "andale mono", "ubuntu mono", monospace; background: rgb(245, 242, 240); color: rgb(0, 0, 0); hyphens: none; line-height: 1.5; box-sizing: border-box; max-width: 100%; overflow: auto; white-space: pre; word-wrap: normal; max-height: 500px; text-shadow: rgb(255, 255, 255) 0px 1px; text-align: left; word-spacing: 0px; word-break: normal; tab-size: 4; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: 0.5px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px;">(a and [b] or [c])[0]

    (b, c)[not a]
    </pre>

    顺便说一下,下面的变体是错误的:

    <pre class=" language-python" style="border: 1px solid rgba(0, 0, 0, 0.0980392); font-size: 15px; font-style: normal; font-weight: normal; margin: 1em 2em; outline: 0px; padding: 1em; vertical-align: baseline; font-family: Consolas, Monaco, "andale mono", "ubuntu mono", monospace; background: rgb(245, 242, 240); color: rgb(0, 0, 0); hyphens: none; line-height: 1.5; box-sizing: border-box; max-width: 100%; overflow: auto; white-space: pre; word-wrap: normal; max-height: 500px; text-shadow: rgb(255, 255, 255) 0px 1px; text-align: left; word-spacing: 0px; word-break: normal; tab-size: 4; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: 0.5px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px;">a and b or c
    True and [] or [1] -> [1], but: [] if True else [1] -> []
    </pre>

    通过列表推导式移除重复的元素

    让我们来把字符串 x = 'tteesstt' 转换成 'test' 吧。

    1.在原字符串中和上一个字符比较:

    <pre class=" language-python" style="border: 1px solid rgba(0, 0, 0, 0.0980392); font-size: 15px; font-style: normal; font-weight: normal; margin: 1em 2em; outline: 0px; padding: 1em; vertical-align: baseline; font-family: Consolas, Monaco, "andale mono", "ubuntu mono", monospace; background: rgb(245, 242, 240); color: rgb(0, 0, 0); hyphens: none; line-height: 1.5; box-sizing: border-box; max-width: 100%; overflow: auto; white-space: pre; word-wrap: normal; max-height: 500px; text-shadow: rgb(255, 255, 255) 0px 1px; text-align: left; word-spacing: 0px; word-break: normal; tab-size: 4; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: 0.5px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px;">''.join(['' if i and j == x[i-1] else j for i,j in enumerate(x)])
    </pre>

    2.把前一个字符保存到临时变量中:

    <pre class=" language-python" style="border: 1px solid rgba(0, 0, 0, 0.0980392); font-size: 15px; font-style: normal; font-weight: normal; margin: 1em 2em; outline: 0px; padding: 1em; vertical-align: baseline; font-family: Consolas, Monaco, "andale mono", "ubuntu mono", monospace; background: rgb(245, 242, 240); color: rgb(0, 0, 0); hyphens: none; line-height: 1.5; box-sizing: border-box; max-width: 100%; overflow: auto; white-space: pre; word-wrap: normal; max-height: 500px; text-shadow: rgb(255, 255, 255) 0px 1px; text-align: left; word-spacing: 0px; word-break: normal; tab-size: 4; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: 0.5px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px;">''.join([('' if i == a else i, [a for a in [i]])[0] for a in [''] for i in x])
    ''.join([('' if i == a.pop() else i, a.append(i))[0] for a in [['']] for i in x])
    </pre>

    3.在新字符串中和上一个字符比较:

    <pre class=" language-python" style="border: 1px solid rgba(0, 0, 0, 0.0980392); font-size: 15px; font-style: normal; font-weight: normal; margin: 1em 2em; outline: 0px; padding: 1em; vertical-align: baseline; font-family: Consolas, Monaco, "andale mono", "ubuntu mono", monospace; background: rgb(245, 242, 240); color: rgb(0, 0, 0); hyphens: none; line-height: 1.5; box-sizing: border-box; max-width: 100%; overflow: auto; white-space: pre; word-wrap: normal; max-height: 500px; text-shadow: rgb(255, 255, 255) 0px 1px; text-align: left; word-spacing: 0px; word-break: normal; tab-size: 4; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: 0.5px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px;">[(not r.endswith(i) and [r for r in [r+i]], r)[-1] for r in [''] for i in x][-1]
    </pre>

    4.通过 reduce 函数和 lambda 表达式:

    <pre class=" language-python" style="border: 1px solid rgba(0, 0, 0, 0.0980392); font-size: 15px; font-style: normal; font-weight: normal; margin: 1em 2em; outline: 0px; padding: 1em; vertical-align: baseline; font-family: Consolas, Monaco, "andale mono", "ubuntu mono", monospace; background: rgb(245, 242, 240); color: rgb(0, 0, 0); hyphens: none; line-height: 1.5; box-sizing: border-box; max-width: 100%; overflow: auto; white-space: pre; word-wrap: normal; max-height: 500px; text-shadow: rgb(255, 255, 255) 0px 1px; text-align: left; word-spacing: 0px; word-break: normal; tab-size: 4; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: 0.5px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px;">reduce(lambda a, b: a if a.endswith(b) else a + b, x)
    </pre>

    通过列表推导式获得斐波拉契数列

    1. 把中间值保存在列表中

    <pre class=" language-python" style="border: 1px solid rgba(0, 0, 0, 0.0980392); font-size: 15px; font-style: normal; font-weight: normal; margin: 1em 2em; outline: 0px; padding: 1em; vertical-align: baseline; font-family: Consolas, Monaco, "andale mono", "ubuntu mono", monospace; background: rgb(245, 242, 240); color: rgb(0, 0, 0); hyphens: none; line-height: 1.5; box-sizing: border-box; max-width: 100%; overflow: auto; white-space: pre; word-wrap: normal; max-height: 500px; text-shadow: rgb(255, 255, 255) 0px 1px; text-align: left; word-spacing: 0px; word-break: normal; tab-size: 4; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: 0.5px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px;">[(lambda: (l[-1], l.append(l[-1] + l[-2]))[0])() for l in [[1, 1]] for x in xrange(19)]
    [(l[-1], l.append(l[-1] + l[-2]))[0] for l in [[1, 1]] for x in xrange(19)]
    </pre>

    1. 把中间值保存到字典中:

    <pre class=" language-python" style="border: 1px solid rgba(0, 0, 0, 0.0980392); font-size: 15px; font-style: normal; font-weight: normal; margin: 1em 2em; outline: 0px; padding: 1em; vertical-align: baseline; font-family: Consolas, Monaco, "andale mono", "ubuntu mono", monospace; background: rgb(245, 242, 240); color: rgb(0, 0, 0); hyphens: none; line-height: 1.5; box-sizing: border-box; max-width: 100%; overflow: auto; white-space: pre; word-wrap: normal; max-height: 500px; text-shadow: rgb(255, 255, 255) 0px 1px; text-align: left; word-spacing: 0px; word-break: normal; tab-size: 4; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: 0.5px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px;">[i for x in [(lambda: (l['a'], l.update({'a': l['a'] + l['b']}), l['b'], l.update({'b': l['a'] + l['b']}))[::2])() for l in [{'a': 1, 'b': 1}] for x in xrange(10)] for i in x]
    [i for x in [(l['a'], l.update({'a': l['a'] + l['b']}), l['b'], l.update({'b': l['a'] + l['b']}))[::2] for l in [{'a': 1, 'b': 1}] for x in xrange(10)] for i in x]
    </pre>

    1. 通过 reduce 函数和 lambda 表达式:

    <pre class=" language-python" style="border: 1px solid rgba(0, 0, 0, 0.0980392); font-size: 15px; font-style: normal; font-weight: normal; margin: 1em 2em; outline: 0px; padding: 1em; vertical-align: baseline; font-family: Consolas, Monaco, "andale mono", "ubuntu mono", monospace; background: rgb(245, 242, 240); color: rgb(0, 0, 0); hyphens: none; line-height: 1.5; box-sizing: border-box; max-width: 100%; overflow: auto; white-space: pre; word-wrap: normal; max-height: 500px; text-shadow: rgb(255, 255, 255) 0px 1px; text-align: left; word-spacing: 0px; word-break: normal; tab-size: 4; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: 0.5px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px;">reduce(lambda a, b: a + [a[-1] + a[-2]], xrange(10), [1, 1])
    reduce(lambda a, b: a.append(a[-1] + a[-2]) or a, xrange(10), [1, 1])
    </pre>

    1. 速度最快的变体:

    <pre class=" language-python" style="border: 1px solid rgba(0, 0, 0, 0.0980392); font-size: 15px; font-style: normal; font-weight: normal; margin: 1em 2em; outline: 0px; padding: 1em; vertical-align: baseline; font-family: Consolas, Monaco, "andale mono", "ubuntu mono", monospace; background: rgb(245, 242, 240); color: rgb(0, 0, 0); hyphens: none; line-height: 1.5; box-sizing: border-box; max-width: 100%; overflow: auto; white-space: pre; word-wrap: normal; max-height: 500px; text-shadow: rgb(255, 255, 255) 0px 1px; text-align: left; word-spacing: 0px; word-break: normal; tab-size: 4; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: 0.5px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px;">[l.append(l[-1] + l[-2]) or l for l in [[1, 1]] for x in xrange(10)][0]
    </pre>

    使用列表推导式产生死循环

    [a.append(b) for a in [[None]] for b in a]

    列表切片技巧

    1. 复制列表:

    <pre class=" language-python" style="border: 1px solid rgba(0, 0, 0, 0.0980392); font-size: 15px; font-style: normal; font-weight: normal; margin: 1em 2em; outline: 0px; padding: 1em; vertical-align: baseline; font-family: Consolas, Monaco, "andale mono", "ubuntu mono", monospace; background: rgb(245, 242, 240); color: rgb(0, 0, 0); hyphens: none; line-height: 1.5; box-sizing: border-box; max-width: 100%; overflow: auto; white-space: pre; word-wrap: normal; max-height: 500px; text-shadow: rgb(255, 255, 255) 0px 1px; text-align: left; word-spacing: 0px; word-break: normal; tab-size: 4; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: 0.5px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px;">l = [1, 2, 3]
    m = l[:]
    m
    -> [1, 2, 3]
    </pre>

    1. 移除/替换 列表中的任意元素:

    <pre class=" language-python" style="border: 1px solid rgba(0, 0, 0, 0.0980392); font-size: 15px; font-style: normal; font-weight: normal; margin: 1em 2em; outline: 0px; padding: 1em; vertical-align: baseline; font-family: Consolas, Monaco, "andale mono", "ubuntu mono", monospace; background: rgb(245, 242, 240); color: rgb(0, 0, 0); hyphens: none; line-height: 1.5; box-sizing: border-box; max-width: 100%; overflow: auto; white-space: pre; word-wrap: normal; max-height: 500px; text-shadow: rgb(255, 255, 255) 0px 1px; text-align: left; word-spacing: 0px; word-break: normal; tab-size: 4; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: 0.5px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px;">l = [1, 2, 3]
    l[1:-1] = [4, 5, 6, 7]
    l
    -> [1, 4, 5, 6, 7, 3]
    </pre>

    1. 在列表的开头添加元素:

    <pre class=" language-python" style="border: 1px solid rgba(0, 0, 0, 0.0980392); font-size: 15px; font-style: normal; font-weight: normal; margin: 1em 2em; outline: 0px; padding: 1em; vertical-align: baseline; font-family: Consolas, Monaco, "andale mono", "ubuntu mono", monospace; background: rgb(245, 242, 240); color: rgb(0, 0, 0); hyphens: none; line-height: 1.5; box-sizing: border-box; max-width: 100%; overflow: auto; white-space: pre; word-wrap: normal; max-height: 500px; text-shadow: rgb(255, 255, 255) 0px 1px; text-align: left; word-spacing: 0px; word-break: normal; tab-size: 4; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: 0.5px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px;">l = [1, 2, 3]
    l[:0] = [4, 5, 6]
    l
    -> [4, 5, 6, 1, 2, 3]
    </pre>

    1. 在列表的尾部添加元素:

    <pre class=" language-python" style="border: 1px solid rgba(0, 0, 0, 0.0980392); font-size: 15px; font-style: normal; font-weight: normal; margin: 1em 2em; outline: 0px; padding: 1em; vertical-align: baseline; font-family: Consolas, Monaco, "andale mono", "ubuntu mono", monospace; background: rgb(245, 242, 240); color: rgb(0, 0, 0); hyphens: none; line-height: 1.5; box-sizing: border-box; max-width: 100%; overflow: auto; white-space: pre; word-wrap: normal; max-height: 500px; text-shadow: rgb(255, 255, 255) 0px 1px; text-align: left; word-spacing: 0px; word-break: normal; tab-size: 4; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: 0.5px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px;">l = [1, 2, 3]
    l[-1:] = [l[-1], 4, 5, 6]
    l
    -> [1, 2, 3, 4, 5, 6]
    </pre>

    1. 反转列表:

    <pre class=" language-python" style="border: 1px solid rgba(0, 0, 0, 0.0980392); font-size: 15px; font-style: normal; font-weight: normal; margin: 1em 2em; outline: 0px; padding: 1em; vertical-align: baseline; font-family: Consolas, Monaco, "andale mono", "ubuntu mono", monospace; background: rgb(245, 242, 240); color: rgb(0, 0, 0); hyphens: none; line-height: 1.5; box-sizing: border-box; max-width: 100%; overflow: auto; white-space: pre; word-wrap: normal; max-height: 500px; text-shadow: rgb(255, 255, 255) 0px 1px; text-align: left; word-spacing: 0px; word-break: normal; tab-size: 4; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: 0.5px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px;">l = [1, 2, 3]
    l[:] = l[::-1]
    </pre>

    替换方法字节码

    Python 阻止替换类实例中的方法,因为 python 给类实例中的方法赋予了只读属性:

    <pre class=" language-python" style="border: 1px solid rgba(0, 0, 0, 0.0980392); font-size: 15px; font-style: normal; font-weight: normal; margin: 1em 2em; outline: 0px; padding: 1em; vertical-align: baseline; font-family: Consolas, Monaco, "andale mono", "ubuntu mono", monospace; background: rgb(245, 242, 240); color: rgb(0, 0, 0); hyphens: none; line-height: 1.5; box-sizing: border-box; max-width: 100%; overflow: auto; white-space: pre; word-wrap: normal; max-height: 500px; text-shadow: rgb(255, 255, 255) 0px 1px; text-align: left; word-spacing: 0px; word-break: normal; tab-size: 4; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: 0.5px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px;">class A(object):
    def x(self):
    print "hello"
    a = A()
    def y(self):
    print "world"
    a.x.im_func = y
    -> TypeError: readonly attribute
    </pre>

    但是可以在字节码的层面上进行替换:

    <pre class=" language-python" style="border: 1px solid rgba(0, 0, 0, 0.0980392); font-size: 15px; font-style: normal; font-weight: normal; margin: 1em 2em; outline: 0px; padding: 1em; vertical-align: baseline; font-family: Consolas, Monaco, "andale mono", "ubuntu mono", monospace; background: rgb(245, 242, 240); color: rgb(0, 0, 0); hyphens: none; line-height: 1.5; box-sizing: border-box; max-width: 100%; overflow: auto; white-space: pre; word-wrap: normal; max-height: 500px; text-shadow: rgb(255, 255, 255) 0px 1px; text-align: left; word-spacing: 0px; word-break: normal; tab-size: 4; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: 0.5px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px;">a.x.im_func.func_code = y.func_code
    a.x()
    -> 'world'
    </pre>

    注意! 这不仅对当前的实例有影响,而且对整个类都有影响(准确的说是与这个类绑定的函数)(译者注:此处应该是笔误,推测作者原意是:准确的说是与这个函数绑定的所有类),并且所有其他的实例也会受到影响:

    <pre class=" language-python" style="border: 1px solid rgba(0, 0, 0, 0.0980392); font-size: 15px; font-style: normal; font-weight: normal; margin: 1em 2em; outline: 0px; padding: 1em; vertical-align: baseline; font-family: Consolas, Monaco, "andale mono", "ubuntu mono", monospace; background: rgb(245, 242, 240); color: rgb(0, 0, 0); hyphens: none; line-height: 1.5; box-sizing: border-box; max-width: 100%; overflow: auto; white-space: pre; word-wrap: normal; max-height: 500px; text-shadow: rgb(255, 255, 255) 0px 1px; text-align: left; word-spacing: 0px; word-break: normal; tab-size: 4; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: 0.5px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px;">new_a = A()
    new_a.x()
    -> 'world'
    </pre>

    让可变元素作为函数参数默认值

    把可变对象作为函数参数的默认值是非常危险的一件事,并且在面试中有大量关于这方面棘手的面试问题。但这一点对于缓存机制非常有帮助。

    1. 阶乘函数:

    <pre class=" language-python" style="border: 1px solid rgba(0, 0, 0, 0.0980392); font-size: 15px; font-style: normal; font-weight: normal; margin: 1em 2em; outline: 0px; padding: 1em; vertical-align: baseline; font-family: Consolas, Monaco, "andale mono", "ubuntu mono", monospace; background: rgb(245, 242, 240); color: rgb(0, 0, 0); hyphens: none; line-height: 1.5; box-sizing: border-box; max-width: 100%; overflow: auto; white-space: pre; word-wrap: normal; max-height: 500px; text-shadow: rgb(255, 255, 255) 0px 1px; text-align: left; word-spacing: 0px; word-break: normal; tab-size: 4; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: 0.5px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px;">def f(n, c={}):
    if n in c:
    return c[n]
    if (n < 2):
    r = 1
    else:
    r = n * f(n - 1)
    c[n] = r
    return r
    f(10)
    -> 3628800
    f.func_defaults
    ({1: 1,
    2: 2,
    3: 6,
    4: 24,
    5: 120,
    6: 720,
    7: 5040,
    8: 40320,
    9: 362880,
    10: 3628800},)
    </pre>

    1. 斐波拉契数列:

    <pre class=" language-python" style="border: 1px solid rgba(0, 0, 0, 0.0980392); font-size: 15px; font-style: normal; font-weight: normal; margin: 1em 2em; outline: 0px; padding: 1em; vertical-align: baseline; font-family: Consolas, Monaco, "andale mono", "ubuntu mono", monospace; background: rgb(245, 242, 240); color: rgb(0, 0, 0); hyphens: none; line-height: 1.5; box-sizing: border-box; max-width: 100%; overflow: auto; white-space: pre; word-wrap: normal; max-height: 500px; text-shadow: rgb(255, 255, 255) 0px 1px; text-align: left; word-spacing: 0px; word-break: normal; tab-size: 4; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: 0.5px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; -webkit-text-stroke-width: 0px;">def fib(n, c={}):
    if n in c:
    return c[n]
    if (n < 2):
    r = 1
    else:
    r = fib(n - 2) + fib(n - 1)
    c[n] = r
    return r
    fib(10)
    -> 89
    fib.func_defaults[0].values()
    -> [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]</pre>

    相关文章

      网友评论

        本文标题:鲜为人知的 Python 语法

        本文链接:https://www.haomeiwen.com/subject/jtngpqtx.html