profile picture

More Than you Ever Wanted to Know about Python Functions

July 26, 2018 - beginner python

What exactly are functions? Let's talk about functions, methods, callables and closures - what they are, what you can give them, what they can give you, what you can do with them ... and what's inside.

You probably think you already know everything about functions, but you probably don't!

Input & Output: How do you get things in and out of functions? I'll cover parameters and the myriad of ways they can be specified, provided and accessed - including helpful hints to avoid common mistakes! I'll cover return values, briefly, along with variable scopes and exceptions.

Closures: What are they, how do they work and how they can affect memory usage.

Methods: How does a method differ from a function, when are they made, how do they work and how to access the function inside every method.

__magic__: Make your own callables from any object!

Introspection: Using modern Python techniques, what can you find out about a function, and what can you do with that information?

Bytecode: What happens if you open up a function and look at its insides? Can you change it and put it back together again? (Spoiler: Yes, you can.)

By the end of this talk, I guarantee* you'll know more about callables than when you walked in, along with techniques both practical and so extreme your colleagues will never let you merge them to master.

(*This guarantee is legally non-binding and cannot be redeemed in any way.)

Notes

This talk was given in Edinburgh, my home town! It was a really large audience, and a great venue, and I had a blast ... but I also ran out of time and had to rush the end of the talk - which is the most difficult bit!

I gave the talk again at PyCon Ireland, but then had massive A/V issues, so I don't really have a better version to link to online. Maybe one day I'll manage to give a great version of this talk in full. Until then, this is what you've got.