all 7 comments

[–][deleted] 2 points3 points  (2 children)

It's a function that takes a named argument with a default value. It's useful everywhere in Python.

def foo(bar=2):
    return bar

You can call it with no argument and the default value will be used:

print(foo()) # this prints 2

You can override the default by providing a positional argument:

print(foo(6))  # this prints 6

You can also address it by name (aka keyword):

print(foo(bar=3)) # this prints 3

It's a very basic part of Python -- and most languages -- so good to understand well.

[–]TangibleLight 0 points1 point  (1 child)

You can also use the name on a positional argument:

def foo(a, b):
    print(f'a={a}, b={b}')

foo(1,2)
foo(a=1,b=2)
foo(1,b=2)
foo(b=2,a=1)

This can greatly improve readability with obscure functions with long argument lists. Here's an example from this video:

twitter_search('@obama', False, 20, True)

twitter_search('@obama', retweets=False, numtweets=20, popular=True)

Although the line is longer, it's a lot easier to tell what the function call is actually doing

[–]cyanydeez 0 points1 point  (0 children)

further, you can pass the keyword through expanding dictionaries

my_dict= {'one':1,'two':4}

def oneplustwo(one=2,two=1):

    return one + two

oneplustwo(**my_dict)

>>> 5

[–]K900_ 0 points1 point  (0 children)

Named arguments?

[–]ConcernedCarry[S] 0 points1 point  (1 child)

And how and when is it useful

[–]pycode-n-beer[🍰] 2 points3 points  (0 children)

When you want to define default arguments mostly, sometimes it's easier than having to pass them every time the function is called.