Python Metaclasses

892 views

Published on

A talk i did at PyConZA 2013

Published in: Technology
3 Comments
1 Like
Statistics
Notes
No Downloads
Views
Total views
892
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
13
Comments
3
Likes
1
Embeds 0
No embeds

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

    1. 1. Python Meta Classes Kisitu Augustine Software Developer at ThoughtWorks Twitter: @austiine04 Github: austiine04
    2. 2. SOME BASICS
    3. 3. Everything is an object in python.
    4. 4. Classes create instances. Class Foo(object): def _ _init_ _(self, bar): self.bar = bar f = Foo(‘Alex Bar’)
    5. 5. type(f)
    6. 6. Creating new types
    7. 7. Class Foo(object): pass
    8. 8. Class Foo: pass
    9. 9. Foo = type(‘Foo’, (), {})
    10. 10. type(cls,*args,**kwargs)
    11. 11. type() is actually not a function. It is a META CLASS.
    12. 12. A special kind of class that creates classes.
    13. 13. type(name, bases, cls_dct)
    14. 14. Class Foo(object): def _ _init_ _(self, bar): self.bar = bar At runtime class Foo is an instance of type
    15. 15. 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
    16. 16. _ _new_ _() vs _ _init_ _()
    17. 17. class Foo(object): _ _metaclass_ _ = Meta def _ _init_ _(self): pass
    18. 18. class Foo(metaclass = Meta): def _ _init_ _(self): pass
    19. 19. Show us the code
    20. 20. Example #1 Making a class final
    21. 21. Example #2 Decorating class methods
    22. 22. def log(function): def wrapper_function(*args, **kwargs): print “Calling ……….”, function.__name__ return function(*args, **kwargs) return wrapper_function
    23. 23. Some advanced basics
    24. 24. A class is an instance of its metaclass at runtime.
    25. 25. Metaclasses go down the inheritance chain.
    26. 26. Things can get quite ugly if you are inheriting from multiple classes each with its own meta class.
    27. 27. With great power comes great responsibility
    28. 28. Questions ???

    ×