Python 2.x gotchas and landmines

后端 未结 23 2193
北恋
北恋 2020-11-28 17:47

The purpose of my question is to strengthen my knowledge base with Python and get a better picture of it, which includes knowing its faults and surprises. To keep things sp

相关标签:
23条回答
  • 2020-11-28 18:17

    List repetition with nested lists

    This caught me out today and wasted an hour of my time debugging:

    >>> x = [[]]*5
    >>> x[0].append(0)
    
    # Expect x equals [[0], [], [], [], []]
    >>> x
    [[0], [0], [0], [0], [0]]   # Oh dear
    

    Explanation: Python list problem

    0 讨论(0)
  • 2020-11-28 18:17

    Python 2 has some surprising behaviour with comparisons:

    >>> print x
    0
    >>> print y
    1
    >>> x < y
    False
    

    What's going on? repr() to the rescue:

    >>> print "x: %r, y: %r" % (x, y)
    x: '0', y: 1
    
    0 讨论(0)
  • 2020-11-28 18:18
    try:
        int("z")
    except IndexError, ValueError:
        pass
    

    reason this doesn't work is because IndexError is the type of exception you're catching, and ValueError is the name of the variable you're assigning the exception to.

    Correct code to catch multiple exceptions is:

    try:
        int("z")
    except (IndexError, ValueError):
        pass
    
    0 讨论(0)
  • 2020-11-28 18:18

    James Dumay eloquently reminded me of another Python gotcha:

    Not all of Python's “included batteries” are wonderful.

    James’ specific example was the HTTP libraries: httplib, urllib, urllib2, urlparse, mimetools, and ftplib. Some of the functionality is duplicated, and some of the functionality you'd expect is completely absent, e.g. redirect handling. Frankly, it's horrible.

    If I ever have to grab something via HTTP these days, I use the urlgrabber module forked from the Yum project.

    0 讨论(0)
  • 2020-11-28 18:18

    If you assign to a variable inside a function, Python assumes that the variable is defined inside that function:

    >>> x = 1
    >>> def increase_x():
    ...     x += 1
    ... 
    >>> increase_x()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 2, in increase_x
    UnboundLocalError: local variable 'x' referenced before assignment
    

    Use global x (or nonlocal x in Python 3) to declare you want to set a variable defined outside your function.

    0 讨论(0)
  • 2020-11-28 18:20

    The values of range(end_val) are not only strictly smaller than end_val, but strictly smaller than int(end_val). For a float argument to range, this might be an unexpected result:

    from future.builtins import range
    list(range(2.89))
    [0, 1]
    
    0 讨论(0)
提交回复
热议问题