zoukankan      html  css  js  c++  java
  • 2017-08-06笔记

    可以使用 ** 操作符分拆关键字参数为字典:
    >>> def parrot(voltage, state='a stiff', action='voom'): ... print("-- This parrot wouldn't", action, end=' ') ... print("if you put", voltage, "volts through it.", end=' ') ... print("E's", state, "!") ... >>> d = {"voltage": "four million", "state": "bleedin' demised", "action": "VOOM"} >>> parrot(**d)

    内建函数 range() 需要要独立的 startstop 参数。你可以在调用函数时加一个 * 操作符来自动把参数列表拆开:
    >>> args = [3, 6]
    >>> list(range(*args)) 

    在迭代过程中修改迭代序列不安全(只有在使用链表这样的可变序列时才会有这样的情况)。如果你想要修改你迭代的序列(例如,复制选择项),你可以迭代它的复本。使用切割标识就可以很方便的做到这一点:
    >>> for w in words[:]:  # Loop over a slice copy of the entire list.
    ...     if len(w) > 6:
    ...         words.insert(0, w)

    list.copy()

    返回列表的一个浅拷贝。等同于 a[:]



    注解是以字典形式存储在函数的 __annotations__ 属性中,对函数的其它部分没有任何影响。参数注解(Parameter annotations)是定义在参数名称的冒号后面,紧随着一个用来表示注解的值得表达式。返回注释(Return annotations)是定义在一个 -> 后面,紧随着一个表达式,在冒号与 ->之间。
    >>> def f(ham: 42, eggs: int = 'spam') -> "Nothing to see here":
    ...     print("Annotations:", f.__annotations__)
    ...     print("Arguments:", ham, eggs)
    ...
    >>> f('wonderful')
    Annotations: {'eggs': <class 'int'>, 'return': 'Nothing to see here', 'ham': 42}
    Arguments: wonderful spam
     
    类似于嵌套函数定义,lambda 形式可以从外部作用域引用变量:
    >>> def make_incrementor(n):
    ...     return lambda x: x + n
    ...
    >>> f = make_incrementor(42)
    >>> f(0)
    42
    >>> f(1)
    43
    

    上面的示例使用 lambda 表达式返回一个函数。另一个用途是将一个小函数作为参数传递:

    >>> pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
    >>> pairs.sort(key=lambda pair: pair[1])
    >>> pairs
    [(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]
     
     
  • 相关阅读:
    Github进不去解决方法
    微信小程序中使用iconfont的方法
    curl实现SFTP上传下载文件
    curl实现SFTP上传下载文件
    curl不支持sftp的解决方法
    sscanf遇到回车换行认为字符串结束,怎样让它忽略这些回车换行继续搜索整个字符串?
    史上最全阿里 Java 面试题总结及答案
    VS Code的Error: Running the contributed command: ‘_workbench.downloadResource‘ failed解决
    云锵投资 2021 年 07 月简报
    看完段永平的100条思考,我终于悟出他为何这么牛…(转载)
  • 原文地址:https://www.cnblogs.com/yingchen/p/7296556.html
Copyright © 2011-2022 走看看