Started up with learning about Python function basics, today. Notes follow. Using the Lerner pool of wisdom, as usual.
Why do we need functions?
- DRY (Don’t Repeat Yourself), because we can take stuff that is repeatable, assign it a nickname and just call that nickname over and over, instead of tediously writing all that stuff over and over and over
- If want to modify something, I can just call it (by the nickname) and then add my little spice to it. Like I have the recipe to an omlette written down somewhere, I can then just add a little note with secret ingredients at the bottom of that recipe to create a better or a different omlette.
Functions are objects.
- We can think of them as verbs
Parentheses in Python (at the end of functions) basically mean, I want to execute, this here thing, on the left
I can run help on a function to get some information about it.help(str.upper)
- We don’t tack parentheses on the function, when I am lookif for its help, because I don’t want to run the darned thing.
Creating/Defining a function
- We define a function using the
def
keyword. It’s followed by the name you want to call it by, followed by parantheses terminated by a colondef hello():
- This is actually a two step thing, even though I’ve always thought of it as one.
- first, I create a function object.
- then, I assign it a name.
- Functions in Python (unlike other languages), have the same name space as the rest of our Python program. I cannot have a function called x and also a variable called x
Returning results
- Similar to math functions, even Python functions return values after running them
- The default is
None
, unless you actually, explicity tell the function to return something - If you want to catch the results of what you are doing with the function, remember to return it.
- A function can return whatever you want. statements, values, other functions etc.
- Which is good and all, but remember to do this with intent. I ought to think about what I want to return and document it well
- If I am returning multiple values of varying types, it comes back as a tuple. Not as different objects. I need to unpack them and assign them to multiple values explicity
Documenting Functions
- The help function needs something to read and tell the user about what our functions do. Ergo …
- Use docstrings (Documentation Strings) to do it.
- What are docstrings? If the first line in a function is a string, then that string is seen by the help system as a docstring and used for documentation.
- Use triple quotes around the string. I can have multiple lines in there
- The convention is to document what a function receives (the input), what it returns (the output) and what it modifies.
def hello():
"""This is it! The docstring! This will help me figure out what the hello function does!
or more seriously
expects: No arguments
modifies: Nothing
returns: A friendly greeting
"""
return 'Hello, hello :)
**Arguments**
Functions need stuff to work with.
We supply them using arguments.
Just a fancy name for the data we pass along to the function.
E.g. If a function returns a fancy format of our name, there must be some mechanism to pass our name across to the function. Arguments are how we do it. We don’t actually argue with the programe :P
A function can take any number of arguments. We just put them between the parentheses, seperated by commas
Positional Arguments
def hello(first, second): print(f'Hello, {first} and {last}')
- In the code above, I can call the function with
hello('Tyche','Hestia')
and it will assign Tyche to first and Hestia to second, because I typed them in that order. - These are called positional arguments
- When I define a number of arguments, I need to pass all the arguments along. I can’t make space for two and pass only one or none.
- I can supply defaults, so that if the user does not supply all the arguments, the default is used in its place
- If some arguments cannot have defaults and are a must, they must come before the ones that are optional and can have defaults.
- How does Python know, how many to expect?
- When Python defines the function, it records the definition and makes a note of what it needs to run the function properly
- If I look at the guts of the
hello
function with ahello.__code__.co_argcount
it’ll tell me, it has recorded two. - If I supply defaults, they are recorded in a seperate place called
hello.__defaults__
and it supplies the requisite information, so theargcount
always stays at the required number of arguments
- If I look at the guts of the
- When Python defines the function, it records the definition and makes a note of what it needs to run the function properly
- In the code above, I can call the function with
A little dazed today, so calling it quits early.
Learning / Feedback/ Experiences from doing exercises
- I keep looking for elegant, one statement like solution to problems and get frustrated. Reuven then shows up with a brutally efficient solution that has four if statements. I should remember to be more workmanlike, before I turn craftsman
Read all about my Python functions journey here