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
  2. 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:

There is an excellent blog describing metaclass mechanism here -> https://blog.ionelmc.ro/2015/02/09/understanding-python-metaclasses/

Regading metaclasses's use in Python patterns, I have covered this in detail in my github repo here -> https://github.com/pythonhacker/designpatterns/blob/master/prototypes/prototype_meta.py, https://github.com/pythonhacker/designpatterns/blob/master/singletons/singleton_call.py and https://github.com/pythonhacker/designpatterns/blob/master/singletons/singleton_new.py

Similar code is also avaialable online in other repos I am sure.

Are we talking of something new here ? The Django one is cool, but its more of a hack than anything else.

Anand B Pillai (~pythonhacker)

Hey ~pythonhacker !

Thanks for expressing interest in my talk. I would gladly go into a bit more detail.

You are absolutely correct that basic examples of metaclass uses can be found on various blog posts. What I would like to cover in my talk is how to use it in production level code and ways to make your code more reusable. For production level examples I want to give examples from libraries that are widely used. In the examples I would like to cover the how metaclass is being used and what would be the alternatives.

Django REST Framework provides an excellent example of initialising attributes at the time of initialisation (As I have covered in my github repo). As for Django, I will have to disagree with you. The Django community chose to go with metaclass after deliberating on the issue for 3 years. You can see in the discussion here that various methods were proposed but none covers all edge cases except for the usage of metaclass. To summarise, the discussion involves proposals of using decorators and overriding __getattribute__. Moreover, scrapy the web scraping library also overrides metaclassto maintain backward compatibility (here), though the approach is a bit different (Django overrides the type class whereas scrapy uses the type function to achieve the same) . I consider django and scrapy to be pretty respectable libraries so I wouldn't consider their approaches a hack.

Another usage of metaclass that I am going to cover is something that I found to be pretty interesting. Covered here in my repo. This provides an alternative way or reusing methods/functions without having to create mixins and inheriting the mixin. I concede the example looks more like an example for over-engineering, however I believe it provides an nice way to reusing custom methods without using mixins.

Vimarsh Chaturvedi (~vimarsh)

Hey ~pythonhacker !

Thanks for expressing interest in my talk. I would gladly go into a bit more detail.

You are absolutely correct that basic examples of metaclass uses can be found on various blog posts. What I would like to cover in my talk is how to use it in production level code and ways to make your code more reusable. For production level examples I want to give examples from libraries that are widely used. In the examples I would like to cover the how metaclass is being used and what would be the alternatives.

Django REST Framework provides an excellent example of initialising attributes at the time of initialisation (As I have covered in my github repo). As for Django, I will have to disagree with you. The Django community chose to go with metaclass after deliberating on the issue for 3 years. You can see in the discussion here that various methods were proposed but none covers all edge cases except for the usage of metaclass. To summarise, the discussion involves proposals of using decorators and overriding __getattribute__. Moreover, scrapy the web scraping library also overrides metaclassto maintain backward compatibility (here), though the approach is a bit different (Django overrides the type class whereas scrapy uses the type function to achieve the same) . I consider django and scrapy to be pretty respectable libraries so I wouldn't consider their approaches a hack.

Another usage of metaclass that I am going to cover is something that I found to be pretty interesting. Covered here in my repo. This provides an alternative way or reusing methods/functions without having to create mixins and inheriting the mixin. I concede the example looks more like an example for over-engineering, however I believe it provides an nice way to reusing custom methods without using mixins.

Vimarsh Chaturvedi (~vimarsh)

Login to add a new comment.