3

I am trying to understand the difference between non-keyword arguments an keyword arguments.

It seems to me that every argument may be also used as a keyword argument.

def print_employee_details(name, age=0, location=None):
    """Print details of employees.

    Arguments:
        name: Employee name
        age: Employee age
        location: Employee location
    """
    print(name)
    if age > 0:
        print(age)
    if location is not None:
        print(location)

Here I have documented the function as having three keyword arguments.

I can choose to call this function without keyword arguments:

print_employee_details('Jack', 24, 'USA')

Or I can call this function with keyword arguments.

print_employee_details(name='Jack', age=24, location='USA')

So it seems to me that documenting all three parameters as keyword arguments as shown below is also fine.

def print_employee_details(name, age=0, location=None):
    """Print details of employees.

    Keyword Arguments:
        name: Employee name
        age: Employee age
        location: Employee location
    """
    print(name)
    if age > 0:
        print(age)
    if location is not None:
        print(location)

So what really is the distinction between normal arguments and keyword arguments?

Lone Learner
  • 18,088
  • 20
  • 102
  • 200
  • You can't call a function with a non-keyword argument without providing an argument value. At the same time opposite is false. You can invoke the function with keyword argument(s) without providing any values (default will be used). – Ivan Velichko Jan 06 '16 at 14:03
  • The real answer (instead of the duplicate), is that what you're documenting are ***parameters***. *Arguments* are the values which you pass at call time! Hence there's a difference between *arguments passed positionally* and *arguments passed by keyword*. No such distinction exists for the *parameters* in the function definition. – deceze Jan 06 '16 at 14:04
  • I would say the main reason to do this is so that you can pass the arguments in any order. That is print_employee_details(age=24, name='Jack', location='USA') does the same as print_employee_details('Jack', 24, 'USA') – Metareven Jan 06 '16 at 14:04
  • 1
    This is a duplicate of http://stackoverflow.com/questions/1419046/python-normal-arguments-vs-keyword-arguments which gives the answers. – sabbahillel Jan 06 '16 at 14:07
  • I looked at that one, and the one I chose seemed more appropriate. – TigerhawkT3 Jan 06 '16 at 14:08
  • @TigerhawkT3 Disagreed. – deceze Jan 06 '16 at 14:09
  • Well, reopen and then close it with the new dup if you feel that strongly about it. – TigerhawkT3 Jan 06 '16 at 14:09
  • 2
    Believe me, I would love to be able to link more than one target, but the system forces me to choose one. As this one asks about usage and "what is the distinction" and the linked dup target is about when to use each style, I felt it was the best fit. Reading through all the results in a page or two of Google results about this (there's a lot of info about this topic) would be the ideal way to get a really good picture of how it works and when to use what. – TigerhawkT3 Jan 06 '16 at 14:15

0 Answers0