• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
 

Python Metaclasses

on

  • 341 views

A talk i did at PyConZA 2013

A talk i did at PyConZA 2013

Statistics

Views

Total Views
341
Views on SlideShare
339
Embed Views
2

Actions

Likes
0
Downloads
7
Comments
3

1 Embed 2

https://twitter.com 2

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

CC Attribution License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

13 of 3 previous next Post a comment

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • 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 Python Metaclasses Presentation Transcript

  • 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 ???