美文网首页
Common Gotchas

Common Gotchas

作者: W_I_S_E | 来源:发表于2017-07-20 22:11 被阅读0次
    1. Mutable default arguments

      A new list is created once when the func is defined, and the same list is used in each successsive call: Python's default arguments are evaluated once when the func is defined, not each time the func is called.

      This means that if you use a mutable default argument and mutate is, you will have mutated that object fro all future calls to the function as well.


      image.png
      image.png

      (But sometimes this gotcha could not be a gotcha,for instance,you want to maintain state between calls of a function like cache)

    2. Late binding closures

    image.png

    What you you should do instead
    the one:

    image.png
    the two:
    image.png
    1. Python package name can not contains '-' character

    2. 在对一个list进行pop操作时,必须是按逆序来删除。即,先删下标大的元素,再删下标小的元素,否则 会越界。

    5.在使用sorted对iterable进行排序的时候,并不会改变传进来的iterable。而是生成一个新的排序好的iterable并return。

    num = 1
    def t():
        print(num)
        # 在进行赋值操作时,解释器默认变量为本地变量
        num += 1
    
    t()
    

    解决办法 声明 global num

    1. decimal module. https://www.v2ex.com/t/415039

    8, 解决 Windows 下 Python 的 too many file descriptors in select() 报错

    相关文章

      网友评论

          本文标题:Common Gotchas

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