SlideShare a Scribd company logo
1 of 28
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

What's hot (9)

Node js Lunch and learn: Session 2
Node js Lunch and learn: Session 2Node js Lunch and learn: Session 2
Node js Lunch and learn: Session 2
 
Python internals and how they affect your code - kasra ahmadvand
Python internals and how they affect your code - kasra ahmadvandPython internals and how they affect your code - kasra ahmadvand
Python internals and how they affect your code - kasra ahmadvand
 
Prototype 패턴 (심만섭)
Prototype 패턴 (심만섭) Prototype 패턴 (심만섭)
Prototype 패턴 (심만섭)
 
Java2
Java2Java2
Java2
 
Python's magic methods
Python's magic methodsPython's magic methods
Python's magic methods
 
Best Guide for Javascript Objects
Best Guide for Javascript ObjectsBest Guide for Javascript Objects
Best Guide for Javascript Objects
 
Java Inner Classes
Java Inner ClassesJava Inner Classes
Java Inner Classes
 
Inheritance
Inheritance Inheritance
Inheritance
 
Pharo Hands-On: 02 syntax
Pharo Hands-On: 02 syntaxPharo Hands-On: 02 syntax
Pharo Hands-On: 02 syntax
 

Viewers also liked

28 a dupla ceifa da terra. apoc. 14.14-20
28   a dupla ceifa da terra. apoc. 14.14-2028   a dupla ceifa da terra. apoc. 14.14-20
28 a dupla ceifa da terra. apoc. 14.14-20
Diego Fortunatto
 
Integrating ADF Mobile with WebCenter
Integrating ADF Mobile with WebCenterIntegrating ADF Mobile with WebCenter
Integrating ADF Mobile with WebCenter
Brian Huff
 
Just away kurzpräsentation_tourismus2020_mayrhofen 2014
Just away kurzpräsentation_tourismus2020_mayrhofen 2014Just away kurzpräsentation_tourismus2020_mayrhofen 2014
Just away kurzpräsentation_tourismus2020_mayrhofen 2014
TourismFastForward
 

Viewers also liked (20)

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

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Recently uploaded (20)

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 

Python Metaclasses

Editor's Notes

  1. Special objects that create classes.
  2. 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.
  3. Explain new style classes in python 2 and how python 3 classes inherit from object implicitly
  4. We call type to determine the type(data type) of an object
  5. Explain how we can use type to create new classes
  6. Python 2 new style classes
  7. Python 3 syntax
  8. 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
  9. Type is the default meta class used to create our classes
  10. When u create a class, that class is an instance of type at runtime.Class Foo: passFoo.__class__
  11. This is the signature of the type meta class constructor.
  12. Calling type on the Foo class will return type
  13. __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
  14. _ _new_ _ is called to create the class while _ _init_ _ is called after the class has been created to initialize the class
  15. Demonstrate inheriting from the bool class. Create a meta class to create a final
  16. Class type on class Foo and Foo.__class__
  17. I think essentially the rule is that the metaclass of the derived class must a subclass of all metaclasses of the bases.