Advanced Python functions by Reuven M. Lerner

Advanced Python functions

Already know how to define and call Python functions? Go deeper, from bytecodes to closures, and truly master them.

Defining functions is just the beginning.

If you have been programming in Python for a while, then you've probably written hundreds, or even thousands, of functions.

In this course, we go behind the scenes to understand what happens when you define a function, and also when you then execute that function.  We'll look at bytecodes, at function objects, at the ways in which Python handles and tracks different types of arguments.  We'll also look at "inner functions" — functions defined inside of other functions — and how Python keeps track of the different variable scopes present in our functions.

We'll also consider the difference between functions and stack frames, and will touch on typing with Mypy, as well.

If you want to understand how Python's functions are defined, stored, and then executed, this is the course for you. You'll learn a great deal that'll help you to write better, more fluent Python code — and thus get more done in less time.

What's included?

Video Icon 37 videos File Icon 5 files


01 Welcome
2 mins
Section 1: Function objects
Section 1 — Function objects.ipynb
97 KB
02 Introduction to function objects
5 mins
03 Function objects and bytecode
8 mins
04 Arguments and local variables
4 mins
05 Disassembling our function
7 mins
06 More arguments
4 mins
07 Defaults
12 mins
08 Disassembling defaults
6 mins
09 *args
7 mins
10 keyword-only parameters
7 mins
11 Exercises 1
3 mins
1.61 KB
12 Exercise 1 solutions
9 mins
13 **kwargs
9 mins
14 When to use **kwargs
8 mins
15 Unpacking *iterables and **dicts
6 mins
16 Writing function wrappers
5 mins
17 Exercises 2
2 mins
1.27 KB
18 Exercise 2 solutions
7 mins
Section 2: Scoping and nested functions
Section 2 — Scoping and nested functions.ipynb
224 KB
19 Exploring __code__
14 mins
20 Scoping — LEGB
18 mins
21 Inner functions.mp4
6 mins
22 Visualizing inner functions
4 mins
23 Enclosing variables
8 mins
24 Using enclosing variables
6 mins
25 nonlocal
10 mins
26 __code__ and enclosing scope
5 mins
27 Multiple nestings
5 mins
28 Use case 1 — Semi-static variables
7 mins
29 Use case 2 — Function factories
4 mins
30 Exercises 3
3 mins
1.71 KB
31 Exercise 3 solutions
6 mins
32 Intro to stack frames
8 mins
33 Functions vs. frames
4 mins
34 The "inspect" module
4 mins
35 Inspecting stack frames
7 mins
36 Type annotations
8 mins
37 Conclusion
2 mins


Who should take this course?

Python developers who are comfortable writing functions, but want to more fully understand and take advantage of what Python has to offer.

What version of Python do you use?

3.7 — I didn't have 3.8 running on my computer when producing the course.

If you have Python 3.6 or above,then you're fine for this course.

Is there a “trial” version or sample?

No, but (a) you can view a number of the videos for free, without purchasing the course, and (b) I offer a 100% guaranteed refund if you need.  

Is automated testing a part of this course?

No, but I am preparing a separate course on "pytest" that will cover functions (among other topics).

I live in Israel. How do you handle issues like VAT?

If you live in Israel, then please don't buy any of my products (including Weekly Python Exercise) via this online platform. I'm obligated to collect VAT from you, and then to pass it along to the government.  Instead, please e-mail me for information about sending me a bank transfer. I'll then add you to the course, but we'll be able to handle it in the standard and legal way. I apologize for this frustration -- believe me, I wish that the platform I use for selling courses would support Israeli VAT, and I keep asking them to do so.

What if I find that the course is too easy or hard, or that I just dislike your jokes?

If you aren't satisfied with my course, then I'll refund your money.  Just e-mail me.  I believe that I'm offering a high-quality product, but I also know that it's not for everyone.

As for the jokes, people have been trying to help me for years. So far, my jokes have just gotten worse, not better. Oh, well.

Do you offer any discounts?

Yes, I offer discounts for students, pensioners/retirees, and anyone living outside of the world's 30 richest countries. E-mail me at for an appropriate coupon code.

As of February 2020, I also offer a significant discount for anyone affected financially by the coronavirus pandemic. Contact me for more details.

I have other questions. What should I do?

Contact me at!