9
votes

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

boto.emr.step.StreamingStep(name, mapper, reducer=None, combiner=None, action_on_failure='TERMINATE_JOB_FLOW', cache_files=None, cache_archives=None, step_args=None, input=None, output=None, jar='/home/hadoop/contrib/streaming/hadoop-streaming.jar')

I know that arguments after mapper have default values so I don't have to specify their values. But what if I want to pass a value for just one argument at the very end? For example, I want to provide values for the name, mapper, and combiner parameters, and just use the default value for reducer.

Should I do this:

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

Or should I expressly pass all arguments before it?

If there are 100 arguments, and I just want to pass a value for the very last argument, then I have to pass many default values to it. Is there a easier way to do this?

4

4 Answers

16
votes

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)
5
votes

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

1
votes
  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.

-1
votes

no, there is a (probably) better way to do this

you can specify which parameter you're declaring when there are default values, you just write it like multiple definitions. for example, say you're calling a function that is defined like this:

def Method(param1=0, param2=0, param3=0, param4=0, param5=0):
    print(str(param1 + param2 + param3 + param4 + param5))

you wanted the parameter param1 to be default (0). you could execute the function like this:

Method(param1= 5, param2= 4, param3= 3, param5= 8)