SlideShare ist ein Scribd-Unternehmen logo
1 von 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 ???

Weitere ähnliche Inhalte

Was ist angesagt? (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
 

Andere mochten auch

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
 

Andere mochten auch (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
 

Kürzlich hochgeladen

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Kürzlich hochgeladen (20)

Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
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...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
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...
 
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...
 

Python Metaclasses

Hinweis der Redaktion

  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.