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?
A type of argument with a default value is a Python optional parameter. A function definition's assignment operator or the Python **kwargs statement can be used to assign an optional argument. Positional and optional arguments are the two sorts of arguments that a Python function can take.
7.1. A function without an explicit return statement returns None . In the case of no arguments and no return value, the definition is very simple. Calling the function is performed by using the call operator () after the name of the function.
Default arguments can be skipped from function call.
How Does __init__() Method Work? The python __init__ method is declared within a class and is used to initialize the attributes of an object as soon as the object is formed. While giving the definition for an __init__(self) method, a default parameter, named 'self' is always passed in its argument.
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)
Just pass the arguments you want by keyword: boto.emr.step.StreamingStep(name='a name', mapper='a mapper', combiner='a combiner')
Do not use positional arguments,instead use keyword arguments.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With