美文网首页
python技巧-强制使用关键词参数[py 3.x]

python技巧-强制使用关键词参数[py 3.x]

作者: reallocing | 来源:发表于2018-12-10 10:10 被阅读0次
    # In Python 3 you can use a bare "*" asterisk
    # in function parameter lists to force the
    # caller to use keyword arguments for certain
    # parameters:
    
    >>> def f(a, b, *, c='x', d='y', e='z'):
    ...     return 'Hello'
    
    # To pass the value for c, d, and e you 
    # will need to explicitly pass it as 
    # "key=value" named arguments:
    >>> f(1, 2, 'p', 'q', 'v')
    TypeError: 
    "f() takes 2 positional arguments but 5 were given"
    
    >>> f(1, 2, c='p', d='q',e='v')
    'Hello'
    

    相关文章

      网友评论

          本文标题:python技巧-强制使用关键词参数[py 3.x]

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