Forced naming of parameters in Python

后端 未结 11 1299
日久生厌
日久生厌 2020-11-27 12:15

In Python you may have a function definition:

def info(object, spacing=10, collapse=1)

which could be called in any of the following ways:<

11条回答
  •  旧时难觅i
    2020-11-27 12:56

    The python3 keyword-only arguments (*) can be simulated in python2.x with **kwargs

    Consider the following python3 code:

    def f(pos_arg, *, no_default, has_default='default'):
        print(pos_arg, no_default, has_default)
    

    and its behaviour:

    >>> f(1, 2, 3)
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: f() takes 1 positional argument but 3 were given
    >>> f(1, no_default='hi')
    1 hi default
    >>> f(1, no_default='hi', has_default='hello')
    1 hi hello
    >>> f(1)
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: f() missing 1 required keyword-only argument: 'no_default'
    >>> f(1, no_default=1, wat='wat')
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: f() got an unexpected keyword argument 'wat'
    

    This can be simulated using the following, note I've taken the liberty of switching TypeError to KeyError in the "required named argument" case, it wouldn't be too much work to make that the same exception type as well

    def f(pos_arg, **kwargs):
        no_default = kwargs.pop('no_default')
        has_default = kwargs.pop('has_default', 'default')
        if kwargs:
            raise TypeError('unexpected keyword argument(s) {}'.format(', '.join(sorted(kwargs))))
    
        print(pos_arg, no_default, has_default)
    

    And behaviour:

    >>> f(1, 2, 3)
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: f() takes exactly 1 argument (3 given)
    >>> f(1, no_default='hi')
    (1, 'hi', 'default')
    >>> f(1, no_default='hi', has_default='hello')
    (1, 'hi', 'hello')
    >>> f(1)
    Traceback (most recent call last):
      File "", line 1, in 
      File "", line 2, in f
    KeyError: 'no_default'
    >>> f(1, no_default=1, wat='wat')
    Traceback (most recent call last):
      File "", line 1, in 
      File "", line 6, in f
    TypeError: unexpected keyword argument(s) wat
    

    The recipe works equally as well in python3.x, but should be avoided if you are python3.x only

提交回复
热议问题