In the previous lecture, we went over the basics of functions. Here, we'll expand a little bit on some of the finer points of function arguments that can both be useful but also be huge sources of confusion. By the end of the lecture, you should be able to
In [2]:
def pet_names(name1, name2):
print("Pet 1: {}".format(name1))
print("Pet 2: {}".format(name2))
pet1 = "King"
pet2 = "Reginald"
pet_names(pet1, pet2)
pet_names(pet2, pet1)
In this example, we switched the ordering of the arguments between the two function calls; consequently, the ordering of the arguments inside the function were also flipped. Hence, positional: position matters.
In contrast, Python also has keyword arguments, where order no longer matters as long as you specify the keyword. We can use the same function as before:
In [3]:
def pet_names(name1, name2):
print("Pet 1: {}".format(name1))
print("Pet 2: {}".format(name2))
Only this time, we'll use the names of the arguments themselves (aka, keywords):
In [4]:
pet1 = "Rocco"
pet2 = "Lucy"
pet_names(name1 = pet1, name2 = pet2)
pet_names(name2 = pet2, name1 = pet1)
As you can see, we used the names of the arguments from the function header itself, setting them equal to the variable we wanted to use for that argument. Consequently, order doesn't matter--Python can see that, in both function calls, we're setting name1 = pet1
and name2 = pet2
.
Keyword arguments are extremely useful when it comes to default arguments.
If you take a look at any NumPy API--even the documentation for numpy.array
--there are LOTS of default arguments. Trying to remember their ordering is a pointless task. What's much easier is to simply remember the name of the argument--the keyword--and use that to override any default argument you want to change.
Ordering of the keyword arguments doesn't matter; that's why we can specify some of the default parameters by keyword, leaving others at their defaults, and Python doesn't complain.
On one hand, you could consider just passing in a single list, thereby obviating the need. That's more or less what actually happens here, but the syntax is a tiny bit different.
Here's an example: a function which lists out pizza toppings. Note the format of the input argument(s):
In [5]:
def make_pizza(*toppings):
print("Making a pizza with the following toppings:")
for topping in toppings:
print(" - {}".format(topping))
make_pizza("pepperoni")
make_pizza("pepperoni", "banana peppers", "green peppers", "mushrooms")
Inside the function, it's basically treated as a list: in fact, it is a list.
So why not just make the input argument a single variable which is a list? Convenience. In some sense, it's more intuitive to the programmer calling the function to just list out a bunch of things, rather than putting them all in a list first. But that argument could go either way depending on the person and the circumstance, most likely.
With variable-length arguments, you may very well ask: this is cool, but it doesn't seem like I can make keyword arguments work in this setting? And to that I would say, absolutely correct!
So we have a slight variation to accommodate keyword arguments in the realm of including arbitrary numbers of arguments:
In [6]:
def build_profile(**user_info):
profile = {}
for key, value in user_info.items():
profile[key] = value
return profile
profile = build_profile(firstname = "Shannon", lastname = "Quinn", university = "UGA")
print(profile)
profile = build_profile(name = "Shannon Quinn", department = "Computer Science")
print(profile)
Instead of one *
in the function header, there are two. And yes, instead of a list when we get to the inside of the function, now we basically have a dictionary!
Arbitrary arguments (either "lists" or "dictionaries") can be mixed with positional arguments, as well as with each other.
In [9]:
def build_better_profile(firstname, lastname, *nicknames, **user_info):
profile = {'First Name': firstname, 'Last Name': lastname}
for key, value in user_info.items():
profile[key] = value
profile['Nicknames'] = nicknames
return profile
profile = build_better_profile("Shannon", "Quinn", "Professor", "Doctor", "Master of Science",
department = "Computer Science", university = "UGA")
for key, value in profile.items():
print("{}: {}".format(key, value))
firstname
and lastname
*nicknames
is an arbitrary list of arguments, so anything beyond the positional / keyword (or default!) arguments will be considered part of this aggregate**user_info
is comprised of any key-value pairs that are not among the default arguments; in this case, those are department
and university
Let's start with an example to illustrate what's this is. Take the following code:
In [15]:
def magic_function(x):
x = 20
print("Inside function: {}".format(x))
x = 10
print("Before function: {}".format(x))
magic_function(x)
print("After function: {}".format(x))
What will the print()
statement at the end print? 10? 20? Something else?
It prints 10. Before explaining, let's take another example.
In [16]:
def magic_function2(x):
x[0] = 20
print("Inside function: {}".format(x))
x = [10, 10]
print("Before function: {}".format(x))
magic_function2(x)
print("After function: {}".format(x))
What will the print()
statement at the end print? [10, 10]
? [20, 10]
? Something else?
It prints [20, 10]
.
To recap, what we've seen is that
Explaining these seemingly-divergent behaviors is the tricky part, but to give you the punchline:
StackOverflow has a great gif to represent this process in pictures:
In pass by value (on the right), the cup (argument) is outright copied, so any changes made to it inside the function vanish when the function is done.
In pass by reference (on the left), only a reference to the cup is given to the function. This reference, however, "refers" to the original cup, so changes made to the reference are propagated back to the original.
Imagine you're throwing a party for some friends who have never visited your house before. They ask you for directions (or, given we live in the age of Google Maps, they ask for your home address).
Rather than try to hand them your entire house, or put your physical house on Google Maps (I mean this quite literally), what do you do? You write down your home address on a piece of paper (or, realistically, send a text message).
This is not your house, but it is a reference to your house. It's small, compact, and easy to give out--as opposed to your physical, literal home--while intrinsically providing a path to the real thing.
So it is with references. They hearken back to ye olde computre ayge when fast memory was a precious commodity measured in kilobytes, which is not enough memory to store even the Facebook home page.
It was, however, enough to store the address. These addresses, or references, would point to specific locations in the larger, much slower main memory hard disks where all the larger data objects would be saved.
Scanning through larger, extremely slow hard disks looking for the object itself would be akin to driving through every neighborhood in the city of Atlanta looking for a specific house. Possible, sure, but not very efficient. Much faster to have the address in-hand and drive directly there whenever you need to.
Think of references as "arrows"--they refer to your actual objects, like lists or NumPy arrays. The name with which you refer to your object is the reference.
In [ ]:
some_list = [1, 2, 3]
# some_list -> reference to my list
# [1, 2, 3] -> the actual, physical list
Whenever you operate on some_list
, you have to traverse the "arrow" to the object itself, which is separate. Again, think of the house analogy: whenever you want to clean your house, you have to follow your reference to it first.
This YouTube video isn't exactly the same thing, since C++ handles this much more explicitly than Python does. But if you substitute "references" for "pointers", and ignore the little code snippets, it's more or less describing precisely this concept.
Some questions to discuss and consider:
1: Give some examples for when we'd want to use keyword arguments, arbitrary numbers of arguments, and key-value arguments.
2: Let's say I wanted to write a function, add1()
, which takes an integer as input and adds 1 to it. We know that integers are passed by value, so therefore any changes made to the argument inside the function are discarded when the function finishes. Are there any additional changes I could make so that this function will indeed give me a value that is 1 + the input argument?
3: There's a slight wrinkle in the pass-by-reference, pass-by-value story: technically speaking, everything in Python is passed by value. The difference comes down to the fact that Python objects are always used in conjunction with their references, while the "primitive" variable types are dealt with directly. Since arguments that are passed by value are always copied, can you explain the process of passing an object to a Python function, and in particular how changes are preserved after the function finishes? (Hint: it really helps to draw pictures when dealing with references and values. Put the references on one side, and the objects they refer to on the other side, then go step-by-step through the process of calling a function, copying the arguments, changing the arguments, and ending the function; the YouTube video in this talk handles this case implicitly near the end!)