2

I am trying to use the function below. Mostly when i call this function I just pass in the report_type but a few of the report calls wants a start_date. Questions

  1. Does the None in the definition of function mean that the field is optional

    def request_report(self, report_type, start_date=None, end_date=None, marketplaceids=()):
    data = dict(Action='RequestReport',
                ReportType=report_type,
                StartDate=start_date,
                EndDate=end_date)
    data.update(utils.enumerate_param('MarketplaceIdList.Id.', marketplaceids))
    return self.make_request(data)
    
  2. I normally call the function with one of the lines below depending on if I am passing a start_date. Other then having a series of 'if' statements for each request type is there a good way to call this function and only pass a specific variable if it is populated? Over time I will be adding more optional parameters and done want a giant 'if' statement

    requested_report=report_api.request_report(report_type=report_name, start_date=starting_date)
    requested_report=report_api.request_report(report_type=report_name)
    
personalt
  • 810
  • 3
  • 13
  • 26
  • So how do you determine that `starting_date` is not needed? Isn't that an `if` statement too? – Martijn Pieters Aug 11 '18 at 15:15
  • for each report type that I request I have a row in a mysql database. So I added a column start_date. Plan to check is string is not empty and then use the code from your answer below – personalt Aug 12 '18 at 00:37

2 Answers2

1

The =... part gives a parameter a default value. Here that's None, and that does mean the parameter is optional when calling the function. If you don't specify it, the default is used. See Default Argument Values in the Python tutorial, as well as the reference documentation for function definitions:

When one or more parameters have the form parameter = expression, the function is said to have “default parameter values.” For a parameter with a default value, the corresponding argument may be omitted from a call, in which case the parameter’s default value is substituted.

When calling such a function, you could pass in the same value as the default, to this specific function that makes no difference; so when there is no starting_date, you can pass in None:

start_date = starting_date or None  # when determining the starting date, default to None
requested_report=report_api.request_report(
    report_type=report_name,
    start_date=starting_date
)

Another option is to set any optional keyword arguments in a dictionary, then pass those in with the ** call syntax to unpack the dictionary as keyword arguments:

kwargs = {}
if ...:  # test that decides if start_date is needed
    kwargs['start_date'] = starting_date  # expression to set the start date

requested_report=report_api.request_report(
    report_type=report_name, **kwargs)

It is fine for the kwargs dictionary to be empty.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

It is called a default parameter.

Default parameter values are evaluated when the function definition is executed

Here is the doc on method definition.
Here is additional information

Kishor Pawar
  • 3,386
  • 3
  • 28
  • 61