Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip providing default arguments in a Python method

Tags:

python

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?

like image 917
Suanmeiguo Avatar asked Feb 11 '14 06:02

Suanmeiguo


People also ask

How do you make an argument optional in Python?

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.

How do you call a function without an argument in Python?

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.

Which argument can be skipped in the function call statement in Python?

Default arguments can be skipped from function call​.

What are the default arguments for __ Init__ method?

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.


3 Answers

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)
like image 84
mhlester Avatar answered Sep 29 '22 10:09

mhlester


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

like image 38
BrenBarn Avatar answered Sep 29 '22 10:09

BrenBarn


  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.

like image 35
DhruvPathak Avatar answered Sep 29 '22 11:09

DhruvPathak