Functions

INPUT · Slides

Naming a value as you pass it

01 / 07

Nobody remembers the order

As the arguments pile up, reading the calling side tells you less and less.

Can you tell that the 15 below is an age? You can, because the definition sits right above it. In a real program that definition lives somewhere else, and not being able to read a call on its own is a problem.

def introduce(name, age, hobby):    print(f"{name} is {age} and likes {hobby}")introduce("Yui", 15, "reading")

Result

Yui is 15 and likes reading

02 / 07

Write the name as you pass it

As you pass a value you can write the name on the receiving side and join it with =. That is a keyword argument (an argument passed by name).

With age=15 written down, you can see at a glance what the 15 is. The line gets a little longer, and in exchange your reader never has to go and find the definition.

def introduce(name, age, hobby):    print(f"{name} is {age} and likes {hobby}")introduce(name="Yui", age=15, hobby="reading")

Result

Yui is 15 and likes reading

03 / 07

The order stops mattering

Pass by name and you can put them in any order you like. Python is never in doubt about which name got which value.

The call below starts with hobby, and the result is exactly the same as before. No more "I think the third one was the hobby".

def introduce(name, age, hobby):    print(f"{name} is {age} and likes {hobby}")introduce(hobby="reading", name="Yui", age=15)

Result

Yui is 15 and likes reading

04 / 07

You can mix in ones without names

You do not have to name them all. Unnamed first, named after, and the two can be mixed.

Leave the obvious ones — the first argument, say — unnamed, and name only the ones whose meaning is hard to catch. Most real Python is written like this.

def introduce(name, age, hobby):    print(f"{name} is {age} and likes {hobby}")introduce("Yui", hobby="reading", age=15)

Result

Yui is 15 and likes reading

05 / 07

An unnamed one cannot follow a named one

Turn the order round and you get an error. Put a named one first and the unnamed one after it has no way of knowing which position it was meant for.

This is a SyntaxError, found before anything runs. It is the error for the shape of what you wrote being wrong.

def introduce(name, age):    print(f"{name} is {age}")introduce(name="Yui", 15)

Result

SyntaxError: positional argument follows keyword argument

06 / 07

So that is what Chapter 2 was doing

You may remember writing sorted(scores, reverse=True) in Chapter 2, with a note that the shape would be covered in Chapter 3. This is it.

reverse is the name of one of sorted's arguments, and True was being passed to that name. scores unnamed, reverse=True named — lined up exactly as the rule says, unnamed first, named after.

scores = [80, 45, 95]print(sorted(scores, reverse=True))

Result

[95, 80, 45]

07 / 07

Other people's code opens up

Python's libraries are built on the assumption that you know this. sorted and split alike have a great many arguments, and most of them get passed by name.

So an unfamiliar something=something holds no fear any more. Read it as "ah, they are passing that one by name".

The next lesson covers its partner — the way you decide what to use when nothing was passed.

text = "apple,orange,grape"print(text.split(sep=","))

Result

['apple', 'orange', 'grape']