0%

编程规范

Python

  • Use ‘’.startswith() and ‘’.endswith() instead of string slicing to check for prefixes or suffixes.

    startswith() and endswith() are cleaner and less error prone:

    Yes: if foo.startswith(‘bar’):

    No: if foo[:3] == ‘bar’:

  • When catching exceptions, mention specific exceptions whenever possible instead of using a bare except: clause:

    1
    2
    3
    4
    try:
    import platform_specific_module
    except ImportError:
    platform_specific_module = None

    *和**的用法

    *args 表示接收一个tuple作为参数;\**kw 表示接收一个dict作为参数

    1
    2
    3
    4
    5
    6
    7
    8
    def sample(a,*args,**kwargs):
    print “a is {}”.format(a)
    print “*args is a tuple {}”.format(args)
    print “**kwargs is a dictionary {}”.format(kwargs)
    >>> sample(1,2,3,4,name=”rahul”,age=26)
    a is 1
    *args is a tuple (2, 3, 4)
    **kwargs is a dictionary {‘age’: 26, ‘name’: ‘rahul’}