How to skip providing default arguments in a Python method

前端 未结 3 1926
抹茶落季
抹茶落季 2020-12-16 22:08

I\'m calling this method from the Python boto2 library:

boto.emr.step.StreamingStep(name, mapper, reducer=None, combiner=None, action_on_failure=\'TERMINATE_         


        
相关标签:
3条回答
  • 2020-12-16 22:33
    1. Do not use positional arguments,instead use keyword arguments.

    2. If it is mandatory to use positional arguments,use *args list, populate it with default values using a loop or something ,and set last argument, then pass it to the method.

    0 讨论(0)
  • 2020-12-16 22:35

    Just pass the arguments you want by keyword: boto.emr.step.StreamingStep(name='a name', mapper='a mapper', combiner='a combiner')

    0 讨论(0)
  • 2020-12-16 22:37

    There are two ways to do it. The first, most straightforward, is to pass a named argument:

    boto.emr.step.StreamingStep(name='a name', mapper='mapper name', combiner='combiner name')
    

    (Note, because name and mapper were in order, specifying the argument name wasn't required)


    Additionally, you can pass a dictionary with ** argument unpacking:

    kwargs = {'name': 'a name', 'mapper': 'mapper name', 'combiner': 'combiner name'}
    boto.emr.step.StreamingStep(**kwargs)
    
    0 讨论(0)
提交回复
热议问题