Slide 1

Slide 1 text

Introduction to MetaClasses Sanyam Khurana ErSanyamKhurana CuriousLearner PyGotham 2020 | New York City | PyGotham TV

Slide 2

Slide 2 text

def __who_am_i__(): • One of you! A part of the community • Goes by “CuriousLearner” over the web / IRC • FOSS contributor • Bug triager for CPython • Contributor to Django and Mozilla’s ecosystem of projects • Maintainer for django-phone-verify

Slide 3

Slide 3 text

MetaProgramming ErSanyamKhurana CuriousLearner

Slide 4

Slide 4 text

MetaClasses ErSanyamKhurana CuriousLearner

Slide 5

Slide 5 text

Everything is an “Object” ErSanyamKhurana CuriousLearner

Slide 6

Slide 6 text

Everything is an “Object” ErSanyamKhurana CuriousLearner int str
 float functions
 class

Slide 7

Slide 7 text

Classes are objects ErSanyamKhurana CuriousLearner

Slide 8

Slide 8 text

Classes are objects ErSanyamKhurana CuriousLearner

Slide 9

Slide 9 text

Classes are objects ErSanyamKhurana CuriousLearner

Slide 10

Slide 10 text

Classes are objects ErSanyamKhurana CuriousLearner Assign class to a variable Copy class Add attributes to a class Pass class as parameters to a function Classes are objects

Slide 11

Slide 11 text

ErSanyamKhurana CuriousLearner Django Phone Verify

Slide 12

Slide 12 text

The esoteric `type` ErSanyamKhurana CuriousLearner • You might already have seen or used type in your Python code

Slide 13

Slide 13 text

ErSanyamKhurana CuriousLearner Classes are objects

Slide 14

Slide 14 text

What’s the type of built-in classes ErSanyamKhurana CuriousLearner

Slide 15

Slide 15 text

The esoteric `type` ErSanyamKhurana CuriousLearner Creating classes using type

Slide 16

Slide 16 text

The esoteric `type` ErSanyamKhurana CuriousLearner Creating classes using type Name of the class to be created

Slide 17

Slide 17 text

The esoteric `type` ErSanyamKhurana CuriousLearner Creating classes using type Name of the class to be created Tuple of all the base classes that the new class should inherit from

Slide 18

Slide 18 text

The esoteric `type` ErSanyamKhurana CuriousLearner Creating classes using type Name of the class to be created Tuple of all the base classes that the new class should inherit from Dictionary of all the attributes of new class in key value pairs

Slide 19

Slide 19 text

The esoteric `type` ErSanyamKhurana CuriousLearner Creating classes using type

Slide 20

Slide 20 text

The esoteric `type` ErSanyamKhurana CuriousLearner Creating classes using type

Slide 21

Slide 21 text

The esoteric `type` ErSanyamKhurana CuriousLearner Creating classes using type

Slide 22

Slide 22 text

The esoteric `type` ErSanyamKhurana CuriousLearner Creating classes using type Both are equivalent

Slide 23

Slide 23 text

How to define metaclass for your class? ErSanyamKhurana CuriousLearner

Slide 24

Slide 24 text

How to define metaclass for your class? ErSanyamKhurana CuriousLearner

Slide 25

Slide 25 text

How to define custom meta class for your class? ErSanyamKhurana CuriousLearner

Slide 26

Slide 26 text

The esoteric `type` ErSanyamKhurana CuriousLearner Creating classes using type That’s the entire magic of dynamic class creation that happens when the interpreter sees the block of code indicating a class. type is the default metaclass

Slide 27

Slide 27 text

The world of Analogies ErSanyamKhurana CuriousLearner child : parent :: parent : grandparent object : class :: class : metaclass

Slide 28

Slide 28 text

What’s the type of type ErSanyamKhurana CuriousLearner

Slide 29

Slide 29 text

What’s the type of type ErSanyamKhurana CuriousLearner

Slide 30

Slide 30 text

What’s the type of type ErSanyamKhurana CuriousLearner

Slide 31

Slide 31 text

What’s the type of type ErSanyamKhurana CuriousLearner

Slide 32

Slide 32 text

What’s the type of type ErSanyamKhurana CuriousLearner Inception

Slide 33

Slide 33 text

What’s the type of type ErSanyamKhurana CuriousLearner

Slide 34

Slide 34 text

What’s a MetaClass : Revisited ErSanyamKhurana CuriousLearner It’s a class factory. Provides a way to create classes on the fly. Let’s you hook-into the class creation logic to validate things ensuring the newly derived class conforms to a certain API standard.

Slide 35

Slide 35 text

Singleton Classes ErSanyamKhurana CuriousLearner

Slide 36

Slide 36 text

Singleton Classes ErSanyamKhurana CuriousLearner

Slide 37

Slide 37 text

Singleton Classes ErSanyamKhurana CuriousLearner

Slide 38

Slide 38 text

Singleton Classes ErSanyamKhurana CuriousLearner

Slide 39

Slide 39 text

Singleton Classes ErSanyamKhurana CuriousLearner

Slide 40

Slide 40 text

Singleton Classes ErSanyamKhurana CuriousLearner

Slide 41

Slide 41 text

Singleton Classes ErSanyamKhurana CuriousLearner

Slide 42

Slide 42 text

Abstract Base Classes ErSanyamKhurana CuriousLearner

Slide 43

Slide 43 text

Abstract Base Classes ErSanyamKhurana CuriousLearner

Slide 44

Slide 44 text

Deeper look at MetaClasses • Called before class body is executed • Must return an instance of dict with local namespace for all the code in the class body. • PEP-3115 — Metaclasses in Python 3000 ErSanyamKhurana CuriousLearner __prepare__ method

Slide 45

Slide 45 text

Deeper look at MetaClasses ErSanyamKhurana CuriousLearner

Slide 46

Slide 46 text

Deeper look at MetaClasses • The __new__ method is the constructor — generates the new instance • __init__ is just a initializer — called after instance is created • __new__ is looked up statically on the instance (class) itself and not up in the hierarchy such as in the metaclass. ErSanyamKhurana CuriousLearner __new__ & __init__ method

Slide 47

Slide 47 text

ErSanyamKhurana CuriousLearner Deeper look at Instance Creation Image Source: https://blog.ionelmc.ro/2015/02/09/understanding-python-metaclasses/

Slide 48

Slide 48 text

ErSanyamKhurana CuriousLearner Deeper look at Class Creation Image Source: https://blog.ionelmc.ro/2015/02/09/understanding-python-metaclasses/

Slide 49

Slide 49 text

ErSanyamKhurana CuriousLearner MetaClass: A deeper look Source: https://github.com/python/cpython/blob/3.9/Lib/enum.py#L122

Slide 50

Slide 50 text

ErSanyamKhurana CuriousLearner MetaClass: A deeper look Source: https://github.com/python/cpython/blob/3.9/Lib/enum.py#L122

Slide 51

Slide 51 text

ErSanyamKhurana CuriousLearner MetaClass: A deeper look Source: https://github.com/python/cpython/blob/3.9/Lib/enum.py#L122

Slide 52

Slide 52 text

ErSanyamKhurana CuriousLearner MetaClass: A deeper look Source: https://github.com/python/cpython/blob/3.9/Lib/enum.py#L122 __iter__

Slide 53

Slide 53 text

ErSanyamKhurana CuriousLearner MetaClass: A deeper look Source: https://github.com/python/cpython/blob/3.9/Lib/enum.py#L122 __iter__

Slide 54

Slide 54 text

ErSanyamKhurana CuriousLearner MetaClass: A deeper look Source: https://github.com/python/cpython/blob/3.9/Lib/enum.py#L122 __iter__ __len__

Slide 55

Slide 55 text

Multiple Inheritance in MetaClasses ErSanyamKhurana CuriousLearner

Slide 56

Slide 56 text

Multiple Inheritance in Metaclasses ErSanyamKhurana CuriousLearner

Slide 57

Slide 57 text

Multiple Inheritance in Metaclasses Only works when you have single root MetaClass from which all base classes inherit from. ErSanyamKhurana CuriousLearner

Slide 58

Slide 58 text

Multiple Inheritance in Metaclasses Only works when you have single root MetaClass from which all base classes inherit from. ErSanyamKhurana CuriousLearner

Slide 59

Slide 59 text

When to use MetaClasses? Before you uses metaclass, ask yourself: - Can I accomplish what I want with a decorator? If yes, then choose decorator. - Am I trying to provide an API for programmers to use (using my library/framework), specifically for derived classes? Would MetaClass reduce the amount of code an application programmer has to write? If yes, then use metaclasses. ErSanyamKhurana CuriousLearner

Slide 60

Slide 60 text

Use cases of MetaClasses • Validation framework for attributes of classes • Hook into the class creation process to control either the inheritance to or fro from the custom class. • Emulating behaviour in other programming languages, such as making a final class as seen in Java. • Hook into the class creation process to maintain order of class definitions (not instance creations) • Controlling meta stuff about the class like enforcing all the private methods of the class to have docstrings. ErSanyamKhurana CuriousLearner

Slide 61

Slide 61 text

References • https://stackoverflow.com/questions/100003/what-are-metaclasses-in- python • https://stackoverflow.com/questions/392160/what-are-some-concrete- use-cases-for-metaclasses • https://eli.thegreenplace.net/2011/08/14/python-metaclasses-by- example/ • https://blog.ionelmc.ro/2015/02/09/understanding-python-metaclasses/ • https://www.python.org/dev/peps/pep-3115/ • https://docs.python.org/3/reference/datamodel.html#customizing-class- creation ErSanyamKhurana CuriousLearner

Slide 62

Slide 62 text

Subscribe for updates: SanyamKhurana.com/python.html ErSanyamKhurana CuriousLearner

Slide 63

Slide 63 text

Many Thanks! Slides available at: https://speakerdeck.com/CuriousLearner/ introduction-to-metaclasses Reach out to me at: [email protected] ErSanyamKhurana CuriousLearner