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')]
     
     
  • 相关阅读:
    VS2015安装水晶报表
    C# 通过java生成的RSA公钥加密和解密
    T4代码生成器
    产品开发- DFX
    读《31天学会CRM项目开发》记录3
    读《31天学会CRM项目开发》记录2
    读《31天学会CRM项目开发》记录1
    产品开发
    产品开发
    机器视觉
  • 原文地址:https://www.cnblogs.com/yingchen/p/7296556.html
Copyright © 2011-2022 走看看