SlideShare una empresa de Scribd logo
1 de 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 ???

Más contenido relacionado

La actualidad más candente

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 2Abhinav Dhasmana
 
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 ahmadvandirpycon
 
Prototype 패턴 (심만섭)
Prototype 패턴 (심만섭) Prototype 패턴 (심만섭)
Prototype 패턴 (심만섭) YoungSu Son
 
Python's magic methods
Python's magic methodsPython's magic methods
Python's magic methodsReuven Lerner
 
Pharo Hands-On: 02 syntax
Pharo Hands-On: 02 syntaxPharo Hands-On: 02 syntax
Pharo Hands-On: 02 syntaxPharo
 

La actualidad más candente (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
 

Destacado

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...GoetheMadrid_Bibliothek
 
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-20Diego Fortunatto
 
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 StudyXING EVENTS
 
What is Digital Signage ?
What is Digital Signage ?What is Digital Signage ?
What is Digital Signage ?seb451
 
Chrisiane Kubrick über Stanley
Chrisiane Kubrick über Stanley Chrisiane Kubrick über Stanley
Chrisiane Kubrick über Stanley Nina Scheu
 
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 aprilSiteVision AB
 
Integrating ADF Mobile with WebCenter
Integrating ADF Mobile with WebCenterIntegrating ADF Mobile with WebCenter
Integrating ADF Mobile with WebCenterBrian Huff
 
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 2015Santos García
 
GESTION DE PROCESOS EN SISTEMAS OPERATIVOS
GESTION DE PROCESOS EN SISTEMAS OPERATIVOSGESTION DE PROCESOS EN SISTEMAS OPERATIVOS
GESTION DE PROCESOS EN SISTEMAS OPERATIVOSomarhcc
 
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 2014TourismFastForward
 
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 ElysAriadneth
 
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 particlesSEENET-MTP
 
Präsentation Archetypen in München
Präsentation Archetypen in MünchenPräsentation Archetypen in München
Präsentation Archetypen in MünchenNeuroversum
 

Destacado (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
 

Último

Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
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...Miguel Araújo
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
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 Nanonetsnaman860154
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
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 productivityPrincipled Technologies
 
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...Drew Madelung
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
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.pptxMalak Abu Hammad
 
[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.pdfhans926745
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 

Último (20)

Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
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 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
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
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
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...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
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
 
[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
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 

Python Metaclasses

Notas del editor

  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.