Python required a variable style

What is the best style for a Python method that requires the keyword argument 'required_arg':

def test_method(required_arg, *args, **kwargs):


def test_method(*args, **kwargs):
    required_arg = kwargs.pop('required_arg')
    if kwargs:
        raise ValueError('Unexpected keyword arguments: %s' % kwargs)

Or something else? I want to use this for all my methods in the future, so I'm looking for best practices for handling the required keyword arguments in Python methods.

+3
source share
3 answers

The first method to date. Why duplicate what the language already offers you?

( * args ** kwargs, ). , (def bar(foo = 0) def bar(foo = None)). def bar(foo = []), , .

+7

; * args . * args , , ?

+2

, **. , -, , .

:

def test_method(*args, **kwargs):
    required_arg = kwargs.pop('required_arg')
    if kwargs:
        raise ValueError('Unexpected keyword arguments: %s' % kwargs)

:

def test_method(required_arg, *args):
    pass
0

Source: https://habr.com/ru/post/1745871/


All Articles