How is __metaclass__ used in real life.

Vimarsh Chaturvedi (~vimarsh)


30

Votes

Description:

Class objects in python are themselves instances of the type class. Usage of the metaclass allows you to modify the process of creating a class object. Practical use-cases use metaclass attribute to add, delete, rename, or substitute methods and attributes in the class that is initiated.

Content:

Theory

  1. Class objects are instances of type class
  2. How to override the type class
  3. Introduction to the __prepare__ method

Examples

  1. How to create singleton classes in Python
  2. Using metaclass to introduce methods in a class without inheritance
  3. How metaclass is used in Django REST Framework Serializers.
  4. How Django uses metaclass to maintain backward compatibility

Prerequisites:

  1. Intermediate familiarity object oriented programming concepts and how inheritance works in Python
    1. Basic understanding of serializers (Not compulsory).

Content URLs:

My content

Other links

  1. invoking metaclass
  2. metaclass docs

Speaker Info:

Vimarsh Chaturvedi is currently working at Hedgehog labs as Developer. Graduated from Delhi Technological University in 2016. Lover of Python and small time contributor to Django REST Framework. In his spare time he likes to get into the nuts and bolts of Django and Django REST Framework. Likes to override DRF functionalities and write mixins on steroids for fun. Firm believer of if you have to write the same code twice there is a more elegant way to do it.

Speaker Links:

Github

Linkedin

Section: Core Python
Type: Talks
Target Audience: Intermediate
Last Updated: