Hidden features of Python

What are the lesser-known but useful features of the Python programming language?

  • Try to limit answers to Python core.
  • One feature per answer.
  • Give an example and short description of the feature, not just a link to documentation.
  • Label the feature using a title as the first line.
  • Quick links to answers:

  • Argument Unpacking
  • Braces
  • Chaining Comparison Operators
  • Decorators
  • Default Argument Gotchas / Dangers of Mutable Default arguments
  • Descriptors
  • Dictionary default .get value
  • Docstring Tests
  • Ellipsis Slicing Syntax
  • Enumeration
  • For/else
  • Function as iter() argument
  • Generator expressions
  • import this
  • In Place Value Swapping
  • List stepping
  • __missing__ items
  • Multi-line Regex
  • Named string formatting
  • Nested list/generator comprehensions
  • New types at runtime
  • .pth files
  • ROT13 Encoding
  • Regex Debugging
  • Sending to Generators
  • Tab Completion in Interactive Interpreter
  • Ternary Expression
  • try/except/else
  • Unpacking+ print() function
  • with statement

  • Chaining comparison operators:

    >>> x = 5
    >>> 1 < x < 10
    True
    >>> 10 < x < 20 
    False
    >>> x < 10 < x*10 < 100
    True
    >>> 10 > x <= 9
    True
    >>> 5 == x > 4
    True
    

    In case you're thinking it's doing 1 < x , which comes out as True , and then comparing True < 10 , which is also True , then no, that's really not what happens (see the last example.) It's really translating into 1 < x and x < 10 , and x < 10 and 10 < x * 10 and x*10 < 100 , but with less typing and each term is only evaluated once.


    Get the python regex parse tree to debug your regex.

    Regular expressions are a great feature of python, but debugging them can be a pain, and it's all too easy to get a regex wrong.

    Fortunately, python can print the regex parse tree, by passing the undocumented, experimental, hidden flag re.DEBUG (actually, 128) to re.compile .

    >>> re.compile("^[font(?:=(?P<size>[-+][0-9]{1,2}))?](.*?)[/font]",
        re.DEBUG)
    at at_beginning
    literal 91
    literal 102
    literal 111
    literal 110
    literal 116
    max_repeat 0 1
      subpattern None
        literal 61
        subpattern 1
          in
            literal 45
            literal 43
          max_repeat 1 2
            in
              range (48, 57)
    literal 93
    subpattern 2
      min_repeat 0 65535
        any None
    in
      literal 47
      literal 102
      literal 111
      literal 110
      literal 116
    

    Once you understand the syntax, you can spot your errors. There we can see that I forgot to escape the [] in [/font] .

    Of course you can combine it with whatever flags you want, like commented regexes:

    >>> re.compile("""
     ^              # start of a line
     [font         # the font tag
     (?:=(?P<size>  # optional [font=+size]
     [-+][0-9]{1,2} # size specification
     ))?
     ]             # end of tag
     (.*?)          # text between the tags
     [/font]      # end of the tag
     """, re.DEBUG|re.VERBOSE|re.DOTALL)
    

    enumerate

    Wrap an iterable with enumerate and it will yield the item along with its index.

    For example:

    
    >>> a = ['a', 'b', 'c', 'd', 'e']
    >>> for index, item in enumerate(a): print index, item
    ...
    0 a
    1 b
    2 c
    3 d
    4 e
    >>>
    

    References:

  • Python tutorial—looping techniques
  • Python docs—built-in functions— enumerate
  • PEP 279
  • 链接地址: http://www.djcxy.com/p/1138.html

    上一篇: VB.NET中是否有条件的三元运算符?

    下一篇: Python的隐藏功能