SlideShare a Scribd company logo
Python Meta Classes
Kisitu Augustine
Software Developer at ThoughtWorks
Twitter: @austiine04
Github: austiine04
SOME BASICS
Everything is an object in
python.
Classes create instances.
Class Foo(object):
def _ _init_ _(self, bar):
self.bar = bar
f = Foo(‘Alex Bar’)
type(f)
Creating new types
Class Foo(object):
pass
Class Foo:
pass
Foo = type(‘Foo’, (), {})
type(cls,*args,**kwargs)
type() is actually not a function.
It is a META CLASS.
A special kind of class that creates classes.
type(name, bases, cls_dct)
Class Foo(object):
def _ _init_ _(self, bar):
self.bar = bar
At runtime class Foo is an instance of type
Defining a meta class
class Meta(type):
def _ _init_ _(cls, name, bases, dict):
pass
def _ _new_ _(meta, name, bases, dct):
pass
def _ _call_ _(cls, *args, **kwargs):
pass
_ _new_ _() vs _ _init_ _()
class Foo(object):
_ _metaclass_ _ = Meta
def _ _init_ _(self):
pass
class Foo(metaclass = Meta):
def _ _init_ _(self):
pass
Show us the code
Example #1
Making a class final
Example #2
Decorating class methods
def log(function):
def wrapper_function(*args, **kwargs):
print “Calling ……….”, function.__name__
return function(*args, **kwargs)
return wrapper_function
Some advanced basics
A class is an instance of its metaclass at runtime.
Metaclasses go down the inheritance chain.
Things can get quite ugly if you are inheriting from
multiple classes each with its own meta class.
With great power comes great
responsibility
Questions ???

More Related Content

PDF
Descriptor Protocol
PDF
Basic file operations CBSE class xii ln 7
PDF
Introduction to type classes in Scala
PPTX
PDF
Introduction to Python-2
PPTX
Lesson 4
PDF
Introduction to Type Script by Sam Goldman, SmartLogic
PPT
Learn c sharp at amc square learning
Descriptor Protocol
Basic file operations CBSE class xii ln 7
Introduction to type classes in Scala
Introduction to Python-2
Lesson 4
Introduction to Type Script by Sam Goldman, SmartLogic
Learn c sharp at amc square learning

What's hot (9)

PPTX
Node js Lunch and learn: Session 2
ODP
Python internals and how they affect your code - kasra ahmadvand
PDF
Prototype 패턴 (심만섭)
PPTX
PDF
Python's magic methods
PPT
Best Guide for Javascript Objects
PDF
Java Inner Classes
PPTX
Inheritance
PDF
Pharo Hands-On: 02 syntax
Node js Lunch and learn: Session 2
Python internals and how they affect your code - kasra ahmadvand
Prototype 패턴 (심만섭)
Python's magic methods
Best Guide for Javascript Objects
Java Inner Classes
Inheritance
Pharo Hands-On: 02 syntax
Ad

Viewers also liked (20)

PPTX
Atravesando fronteras: los programas de promoción a la traducción desde la Bi...
DOC
28 a dupla ceifa da terra. apoc. 14.14-20
PPTX
Jalan menuju iman
PPS
MESSAGES
PDF
Thuban talleres karma
TXT
A spectos tecnicos
PDF
Twitter for Events - amiando Social Media Study
PDF
What is Digital Signage ?
PDF
Chrisiane Kubrick über Stanley
PDF
17 de marzo del 2014
PDF
Så lyckas du med ditt sociala intranät - Webbinarium den 15 april
PPT
Integrating ADF Mobile with WebCenter
PDF
Dalí
PPTX
Presentacion Air France KLM Delta Alitalia, Mayo 2015
PPT
GESTION DE PROCESOS EN SISTEMAS OPERATIVOS
PPTX
Introduction to drama
PDF
Just away kurzpräsentation_tourismus2020_mayrhofen 2014
PPTX
Rubéola y Caso Clínico - Elyz A. Cortez López
PDF
Dumitru Vulcanov - Master degree program Astrophysics and elementary particles
PDF
Präsentation Archetypen in München
Atravesando fronteras: los programas de promoción a la traducción desde la Bi...
28 a dupla ceifa da terra. apoc. 14.14-20
Jalan menuju iman
MESSAGES
Thuban talleres karma
A spectos tecnicos
Twitter for Events - amiando Social Media Study
What is Digital Signage ?
Chrisiane Kubrick über Stanley
17 de marzo del 2014
Så lyckas du med ditt sociala intranät - Webbinarium den 15 april
Integrating ADF Mobile with WebCenter
Dalí
Presentacion Air France KLM Delta Alitalia, Mayo 2015
GESTION DE PROCESOS EN SISTEMAS OPERATIVOS
Introduction to drama
Just away kurzpräsentation_tourismus2020_mayrhofen 2014
Rubéola y Caso Clínico - Elyz A. Cortez López
Dumitru Vulcanov - Master degree program Astrophysics and elementary particles
Präsentation Archetypen in München
Ad

Similar to Python Metaclasses (20)

PDF
Metaclasses – Python’s Object-Oriented Paradigm and Its Metaprogramming
DOCX
Python Metaclasses
PDF
A peek into Python's Metaclass and Bytecode from a Smalltalk User
PDF
Understanding Metaclasses
PPTX
Python_Object_Oriented_Programming.pptx
PDF
Induction in metaclasses
PDF
Metaclass Programming in Python
PPT
Lesson on Python Classes by Matt Wufus 2003
PDF
Advanced Python, Part 1
PDF
Object_Oriented_Programming_Unit3.pdf
PPTX
The Awesome Python Class Part-4
PPTX
My Object Oriented.pptx
PDF
Object oriented Programning Lanuagues in text format.
PPTX
Python programming computer science and engineering
PPTX
Object Oriented Programming.pptx
PPTX
Module-5-Classes and Objects for Python Programming.pptx
PPTX
Python lec4
PPT
Chap 3 Python Object Oriented Programming - Copy.ppt
PDF
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
PDF
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
Metaclasses – Python’s Object-Oriented Paradigm and Its Metaprogramming
Python Metaclasses
A peek into Python's Metaclass and Bytecode from a Smalltalk User
Understanding Metaclasses
Python_Object_Oriented_Programming.pptx
Induction in metaclasses
Metaclass Programming in Python
Lesson on Python Classes by Matt Wufus 2003
Advanced Python, Part 1
Object_Oriented_Programming_Unit3.pdf
The Awesome Python Class Part-4
My Object Oriented.pptx
Object oriented Programning Lanuagues in text format.
Python programming computer science and engineering
Object Oriented Programming.pptx
Module-5-Classes and Objects for Python Programming.pptx
Python lec4
Chap 3 Python Object Oriented Programming - Copy.ppt
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf

Recently uploaded (20)

PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Encapsulation theory and applications.pdf
PDF
Empathic Computing: Creating Shared Understanding
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Big Data Technologies - Introduction.pptx
PDF
Machine learning based COVID-19 study performance prediction
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Cloud computing and distributed systems.
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Encapsulation_ Review paper, used for researhc scholars
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Per capita expenditure prediction using model stacking based on satellite ima...
Network Security Unit 5.pdf for BCA BBA.
Chapter 3 Spatial Domain Image Processing.pdf
Encapsulation theory and applications.pdf
Empathic Computing: Creating Shared Understanding
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
Big Data Technologies - Introduction.pptx
Machine learning based COVID-19 study performance prediction
The AUB Centre for AI in Media Proposal.docx
MYSQL Presentation for SQL database connectivity
Cloud computing and distributed systems.
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Building Integrated photovoltaic BIPV_UPV.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Encapsulation_ Review paper, used for researhc scholars
“AI and Expert System Decision Support & Business Intelligence Systems”
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
20250228 LYD VKU AI Blended-Learning.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy

Python Metaclasses

Editor's Notes

  • #2: Special objects that create classes.
  • #4: A little bit of live code to define a function called foo and assign it to a variable and pass it around as an argument.
  • #5: Explain new style classes in python 2 and how python 3 classes inherit from object implicitly
  • #6: We call type to determine the type(data type) of an object
  • #7: Explain how we can use type to create new classes
  • #8: Python 2 new style classes
  • #9: Python 3 syntax
  • #11: Explain how we can use type to determine the data type or class of a python object for example call type on the sys module
  • #12: Type is the default meta class used to create our classes
  • #13: When u create a class, that class is an instance of type at runtime.Class Foo: passFoo.__class__
  • #14: This is the signature of the type meta class constructor.
  • #15: Calling type on the Foo class will return type
  • #16: __new__() should be overidden when you want to control the creation of the object which happens to be the class__init__() should be overidden when you want to control initiailization of the object after it has been created.__call__() is called after class creation, when the already created class is called to create instances
  • #17: _ _new_ _ is called to create the class while _ _init_ _ is called after the class has been created to initialize the class
  • #21: Demonstrate inheriting from the bool class. Create a meta class to create a final
  • #25: Class type on class Foo and Foo.__class__
  • #27: I think essentially the rule is that the metaclass of the derived class must a subclass of all metaclasses of the bases.