Abstract Base Classes: a smart use of metaclasses

Video thumbnail (Frame 0) Video thumbnail (Frame 1110) Video thumbnail (Frame 2459) Video thumbnail (Frame 3282) Video thumbnail (Frame 3962) Video thumbnail (Frame 4627) Video thumbnail (Frame 5625) Video thumbnail (Frame 6471) Video thumbnail (Frame 7692) Video thumbnail (Frame 8320) Video thumbnail (Frame 9705) Video thumbnail (Frame 12792) Video thumbnail (Frame 13798) Video thumbnail (Frame 19611) Video thumbnail (Frame 34235) Video thumbnail (Frame 35029) Video thumbnail (Frame 39585) Video thumbnail (Frame 40678) Video thumbnail (Frame 41991) Video thumbnail (Frame 42987) Video thumbnail (Frame 44045)
Video in TIB AV-Portal: Abstract Base Classes: a smart use of metaclasses

Formal Metadata

Abstract Base Classes: a smart use of metaclasses
Title of Series
CC Attribution - NonCommercial - ShareAlike 3.0 Unported:
You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal and non-commercial purpose as long as the work is attributed to the author in the manner specified by the author or licensor and the work or content is shared also in adapted form only under the conditions of this license.
Release Date

Content Metadata

Subject Area
Abstract Base Classes: a smart use of metaclasses [EuroPython 2017 - Talk - 2017-07-11 - PythonAnywhere Room] [Rimini, Italy] This talk shows what Python Abstract Base Classes (ABCs) are and how they can be used to categorise objects and implement a virtual inheritance tree. The goal of the talk is to introduce programmers to the concept of ABCs, and to show that metaclasses are not a terribly complex topic, but something that can be used by normal programmers. The talk is divided in three different “levels of difficulty”, each of one going deeper in the technical aspects of the subject. Level 1: Polymorphism - Behaviours and delegation - Collections The first level is meant to give a quick overview of the collections package and some useful code snippets. Even beginners can benefit of the information given here. To complete this level you only need to know what a try/except block is. Level 2: Registering - Abstract Base Classes - Categories The second level shows what real and virtual inheritance are, explains what is the meaning of registering and defines Abstract Base Classes. To complete this level you need to know what class inheritance is (i.e. what class MyList(list) means). Level 3: Build your ABCs - Metaclasses - ABSs as interfaces The third level shows how to create your own ABCs, why metaclasses are involved and why they are not a difficult concept. I will then quickly review a possible use of metaclasses as interfaces. To complete this level you need to know how to instantiate a class, and the difference between class and instance
Inheritance (object-oriented programming) Moment (mathematics) Energy level Natural language Abstraction
Polymorphism (materials science) Polymorphism (materials science) Interface (computing) Sheaf (mathematics) Ultraviolet photoelectron spectroscopy Bit Parameter (computer programming) Computer programming Formal language Object-oriented programming Quadratic equation Interface (computing) Energy level Social class Energy level Abelian category Abstraction Data structure Social class
Object-oriented programming Parameter (computer programming) Data structure Instance (computer science) Data structure Social class
Slide rule Word Arm Software testing Damping Right angle Quicksort Data structure Element (mathematics) World Wide Web Consortium
Type theory Functional (mathematics) State of matter View (database) Software testing Social class Quicksort Data structure Social class Spacetime
Functional (mathematics) View (database) Social class Electronic mailing list Object (grammar) Social class
Complex (psychology) Slide rule Price index Line (geometry) Object (grammar) Data structure Code Data structure
Tuple Code Mereology Parameter (computer programming) Instance (computer science) Sequence Inclusion map Type theory Message passing Arithmetic mean Personal digital assistant String (computer science) String (computer science) Phase transition Summierbarkeit Object (grammar) Social class
Functional (mathematics) Code Software developer Basis <Mathematik> Bit Instance (computer science) Mereology Revision control Optical disc drive Mechanism design Operator (mathematics) Core dump Revision control Data conversion Object (grammar) Perfect group Social class
Revision control Inheritance (object-oriented programming) Revision control Social class Line (geometry) Instance (computer science) Object (grammar) Social class
Greatest element Inheritance (object-oriented programming) Run time (program lifecycle phase) Link (knot theory) Code Mereology Power (physics) Internetworking String (computer science) Implementation Social class Condition number Module (mathematics) Key (cryptography) Mapping Inheritance (object-oriented programming) Real number Lemma (mathematics) Projective plane Constructor (object-oriented programming) Basis <Mathematik> Subgraph Instance (computer science) Sequence Type theory Arithmetic mean Loop (music) Revision control Video game Social class Right angle Object (grammar) Library (computing) Asynchronous Transfer Mode
Tuple Meta element Greatest element User interface Java applet View (database) Multiplication sign Execution unit Workstation <Musikinstrument> Range (statistics) Instance (computer science) Correlation and dependence Parameter (computer programming) Mereology Programmer (hardware) Mechanism design Coefficient of determination Different (Kate Ryan album) Personal digital assistant Cuboid Monoidal category Abstraction Social class Mapping Concentric Building Keyboard shortcut Interface (computing) Menu (computing) Instance (computer science) Sequence Type theory Quadratic equation Interface (computing) Right angle Energy level Quicksort Abelian category Directed graph Slide rule Inheritance (object-oriented programming) Link (knot theory) Maxima and minima Distance Attribute grammar Power (physics) Cross-correlation Object-oriented programming String (computer science) Data structure output Associative property Abelian category Module (mathematics) Inheritance (object-oriented programming) Forcing (mathematics) Interface (computing) Lemma (mathematics) Planning System call Compiler Personal digital assistant Social class Game theory Object (grammar) Abstraction Tuple
Meta element Default (computer science) Implementation Inheritance (object-oriented programming) Information Electronic mailing list Interface (computing) Bit Correlation and dependence Sequence Formal language Category of being Type theory Personal digital assistant String (computer science) Quadratic equation Interface (computing) Flag Energy level Social class Abelian category Social class
Tuple Multiplication Digital electronics Information Inheritance (object-oriented programming) Euler angles Building Sequence Revision control Personal digital assistant Triangle Social class Right angle Object (grammar) Figurate number output Abstraction Social class
Point (geometry) Multiplication Inheritance (object-oriented programming) Building Social class Right angle Bit output Abstraction Theory
thank you thank you I thank you for being here um yeah so today I will talk about was the abstract base classes and why I think they are useful and usable thing to know about and 1st of all that's me too much that's so here are recap at the ends of um I want just to uh point out that I also works sometimes and so uh I'm mom levels at the moment I'm just doing a lot of it appears in genetics modem vital
but today talk um is divided into 3 levels so all sections so um but so during that the 1 I will talk about polymorphism the aliasing litigation and collections so some concept that I think we should know has Python parameters programs in general and collections is a module uh that is worth knowing in Python and in that 2 I will talk about reducing ups of these classes finally and categories and of 3 uh I would go a bit deeper into the topic and talk about how you build your own um Madison glasses metaclasses and how to use them as interfaces so um item is our language that's pushes the
object-oriented programming um methodology a
lot so 1 of the tenets of object-oriented programming is that behavior of over structure so you will we as fighting parameters we shoots uh consider behavioral of classes more than the structure what they're doing then at the end of the day I'm not interested in knowing if something is at least or other things
dressing knowing if it behaves like a base so I'm interested in knowing if you class you instance um can be sliced if I cannot
stand uh the elements I Don I don't I'm not interested the sort of annoying if you he married from these or if you implemented from scratch and this is a reason why in
Python we push the year if the arms body can a lot so it's easier to ask for forgiveness than permission rights so we try and accepts we try to use something and then we accept because we are not checking if some OBJ in this slide the uh the subscript words you know if if it's a least this is something that can be as lies not we just right and if it doesn't
work we do something and this is and we do this because really interesting the BAU this is an example of behavior um I
have 2 classes this view and item you sort of like jungle views they inherit from our current class which is view and the pun class doesn't define sorry I didn't have enough space in this type of the and find cost doesn't define the status function the 2nd state as method and I implemented directly in the this view and that item you so there is no way
for me to know to to say that least view and I can view implement the function the I mean I cannot say they you know it from view this is not a problem because you doesn't define the status of us this acres method so this is an example of behavioral for me those 2 classes behavior some way they provide a method so the question is how we how do we check
on the an object behaves and 1 thing that we can do is this 1 so we can try and call the method for example please note that I'm not calling the status that I don't have the parentheses and just asking for the method of it so I'm doing it because I hope by what
I 0 I know that bite on raises the outer Botero if there is some OBJ doesn't contain the status method right but they not actually using it so this is how well this works uh I don't really like it because it's like 3 lines of codes to adjust perform from 1 check and doesn't that she doesn't do anything more than that if I have to check for complex
behavioral like for example my object behaves like a least because every complex hybrid which have the slides have to append things whatever I have to check 3 methods and must be more than that and this is well ahead of me unattainable and st and wrong because I am checking the structure of an well in Python there are
ways to provide methods without actually uh putting them into classes so um I would say this is in general not the other good solution I don't like it as a parameter the temptation uh we haven't probably already used it a lot of is to use is instance they can save if the sum of J is an instance of least in this case I actually check that might be dangerous least it is honest and this is wrong because it is checking the type of the object what if I implement a new class that behaves like a least but is not police this phase this this doesn't pass but the code works more than that if I start saying well I want an
object which is on these properly or string because you know I'm not just taken from mutable sequence in this case a means checking for something that can be sliced and the 3 things the assisting and topple can be sliced and then he as say so meant to the new toppled the whatever and they have to out it's so it becomes a maintainable very
quick the perfect solution in my opinion would be to have something like behaves like it's a function that saves yes you're OBJ behaves like this behavior when this behavior is a class in my dream that's describes a behavior appendices yeah performs these operations those designed and that the the OK good news we have something like that but 1st of all I can is based on delegation this is a are very important things to know about items that's a lot of things in Brighton are implemented by duplicating thing for example if you are a familiar I think we do magic metals of fighter like Dundar odd that under it the other than that contains the weather and this is a litigation because I'm not providing some code in Python core that sees that objects come is lies all contain something or can be added it's the object itself the class industries that provide some into this easier can be to whatever why can this last and this is important because it actually the mechanism that core developers um never 2 implements a solution to my problems of my behavior checking function this is the original version of these instance of some by conversions at all whenever a simplified version of this thing but the concept is that's what I'm doing is to check I'm busting a class
and then checking that the class uh is being there some basis of my object basis is our internal uh attibutes that these all the parts of a lecture the original version is a bit more complex because we
have to somehow climb through the and um inheritance 3 OK but this is the concept line checking and saying yeah Piketon does dates is instance checks for the presence of the class the the was changed to 1 new version which is based on litigation I gained a simplified version is 1st now status that
object is an instance of a class if that object say's yes I am an instance of that class so the object so that forget to the class C yet a method which is called instance checking that except your object and checks for something and that
checks for something is extendable I can create a new idea it's OK so this is not provided by Python in the course of so now this is possible is is passed it is possible to say if the easiest us some of the OBJ this behavior because it is in sense um uses the Internet instance check the method of least behavior we just need to create some class that this behavior that can check for that right the the and the collections and collections is a module that you can find in the standard library the Urban yeah it's a module of classes that has a erupted that represent the interests and behaviors like mutable sequence sequence uh mapping what whatever they go and check the documentation this is an example I Input collection I say is a string of a sequence of distinct is a sequence from his life for example so the check is to but that a string is not the mapping is often dictionary right that's no key value text inside so the check false and the link there is the the condition of the collections mode you by my advice is to go check it and start using it because it's a very powerful with you it that it doesn't happen that's some of that often in Python that you have to check we check of uh different very different behaviors but sometimes heads uh my code to the needs of check for projection for example something was a string or a least it happens sometimes with eyes I asked 2nd on a simple object that's a string of text of all of the stuff text and you cannot tell them apart by just trying to slice them because if you will if you will create a for loop on your in of our new apartment the phone upon this thing works because that the stink is that is that constructs such to level to um yes what is the meaning of being a subclass invited but if you know some vitamin you could say well being a subclass is something that you can do when you inherit from another class and actually this is do it's 1 of the 2 methods uh this is what we call real subgraphs you he narrates from our class so child class notes from prying plus and their memorization should be to the 2 classes is very important it's on the bottom to try class who knows by class if you have an instance of type class you can go and check runtime that power class is the part it's in the basis of 2 but by class doesn't know anything about take us is in is possible tyrant OK just sponsors should turn and doesn't know anything about them so you cannot go to buy class and ask can you tell me all you children in my code base the doesn't work the 2nd method is to be obvious to subclass this could be new to you and actually is called the registering in the object-oriented or
the in Python as well what you do is to register or adjustment
problem the child class into the and class and in this case the relationship is the opposite bind class knows that child class is a is a child that sort of but shy classes in or anything about you're just assigned Chang class 2 apparent and this is important because of classes that can register other classes are called abstract base classes so this is the definition for today still enough base class is something that can register and other class and in that collections module called you can go and check it on time I found that this we call this financing which are very interesting because it saves a sequence which is a class I'm this class the finding that module stays high register couple of tuple I register string and the register range for example so those freaking things our sequences because sequence registers them and a ys is also registered as a mutable sequence which is not a sink is not me was the impact 1 very important thing to uh remember is that the richest registering is a promise so Python doesn't know absolutely no check but if you're adjust that sorry I go back if you say sequence register topple so there is no check done Python about topic so that provides a semantic frightening couple has some is no it's really just a promise the an example of here high import collections I define a class my class which is empty and doesn't inherit from anything about object so it which empty if I piece shoe is subclass my class of collection sequence obviously I get the falls but this is not the least but here that take I register Tracy collection sequence register my class I say my class is it behaves like a sequence and bottom dutifully face it's true OK so it's a very powerful mechanism but we get power concentrators possibility right of base classes are categories just labels OK dogs if you want it's a way to say yes this class behaves like that but is just some you know it's a promise I'm and tagging my class as our sequence or mutable and dictionary a mapping or whatever you are promising yourself for the other programmers that you're classes behaving like you want your like you promise on to level 3 however do you build your own absent discusses an this is from the docks because the the is of moves that is known as not wars implementing it ourself from ABC which is the module that defines parts of the classes import ABC method which is a method metaclass more on that in 1 I define a class which is just a plane and to class might have in mind my ABC and they define the metaclass of that class is ABC method is just called OK my class can be tempted and then I can use magically so use register 2nd say for example my ABC register couple and what happens is that they can say that Apple is a subclass of might they be seen because any registered and they can say that any instance of toppled like that you bring this is there are an instance of my ABC so it's pretty easy to define and ABC why metaclasses the the 1st of all don't plan OK there's a reason why work station the uh the classes are used by Crichton parameter sketches and in the nite and they don't go to bed rights so I want to show you that met the classes are not complex not more difficult than classes here I have I'm very simple bite called I think it's a classification an association of that class and the use of distance so book is up class that defines to just go in an attribute tensor fortitude when I'm instantiate the book I can use the instances and say these instance give me the answer and the answer for pretty easy I think everyone here understands this cults this is the use of a metaclass I define the metaclass which is a class from types as an units which some parameters and I can define something inside so for example so far answer voted sounds familiar the I can't link a class the metaclass 2nd say my class book has come at the class and they can use the class and say now this class give me the answer and the answer is a game for 2 2 I want to compare that to so on the left you have the class instantiation on the right you have the metaclass if initial and I profane they're pretty much the same thing so you have a definition of a class 1 press you ever use of the class or the metaclass and you have I mean Sister of the thing why because metaclasses so classes in part of our ideas right so it's something that is is sort of a complete over concrete instances instance and the metaclass is the same thing is the idea is that of a class I want the class to behave some in some way the class not doing instance I create them at the cross I think it's pretty easy if you know how to create an instance from a class you know how to create a class from a medical costs the and why yeah it is simple in my opinion why are we using method classes so where for instance because we need to the classes to contain this a register a method yeah we started from the need of having a register method in the class who won the class to be able to register other classes because we want to tag them right like stuff metaclasses come put things into the classes so we use the method has to say now I get my ABC my my ABC class and they put inside the register method and magically I have it because of the medical metaclass and once Alex method which is and who is here uh ask me why don't we we in that we use um decorators for example can be done I decorate my ABC class and I put the register uh method inside well the corre tools can be in there it can't be negative so if you inherit from a class that is decorated you dont he died in the the greater but you inherit the metaclass so metaclasses are more powerful in this case then the greatest the or yeah sorry I have of light on your you know the metaclass year you see the for the fine may and my ABC which is on ABC method and then other ABC which inherits from my A. B. C. and they can use other ABC just like a use my ABC so inheritance works here it's important the last 2 slides and are about our different views of aversive base classes I'm not very fond of it but it's worth mentioning it because it's into the commendation you can use absent base classes to be and interfaces if you are familiar with other object-oriented programming languages like Java for example you know that there's a concept called interface which is something that and forces are structure of the object so for example you save my uh class has to contain the status method In the end in Java if you do this and these are the compiler complains if you don't think that the matter to present by In bite on um we for many years we didn't have that's and now we can do it so we do up some make the method the correlation the corre sorry which is the finding the ABC the
module so you just define your class like we you did before it's an ABC matter OK you can put an such methods which is something that gets uh checked that when you instantiate the class so in the red box and you see what happens if you tried to instantiate the US the status interface this season TypeError because you cannot instantiate an abstract class why is not offset that's because doesn't contain an
implementation of this adjustment is flagged as an absolute metal and if you inherit FA-status interface in this case and you don't define the status method fight on complaints with the type of saying you cannot instantiated you have to do it the I'm not particularly fond of it but it's my personal opinion because I think Brighton is a very very dynamic language sorry strongly believe in providing behaviors reflecting things and not enforcing uh the presence of metals but he can be useful so if you want to use it go and check the documentation because there other the corre toast offset property for example out youth and so on so it and that's all the if B questions was it too fast story to the eye but if default In the beginning a briefly mention that it's not possible to this allows the others can I make them chemical In the beginning you mention that it's not as easy to distinguish between string like things and lives like things you mentioned an API example that takes it string for a list of strings is that it's not possible when they when the bit deeper Cape I give advice on what is the proper approach to do this without falling back to like the ugly type checks as you said yeah well actually it's an example of a the well I had to implement it and you can do it yeah for example if you use collection sequence that doesn't work right because the string and the least of both sequences that you can use mutable sequence because this is mutable and is not obviously if you will if you need to to uh in tell apart to things that are mutable and sequences you cannot use collections by 2 if you define something which is like a least but is different and you want to tell a least and Europe class apart and you have to define your own answer base class and say OK a flag my legal my classes the long what's my category and then I checked that from the collections you can do it with the mutable sequence so uh yeah multiple sequence this but is it to the yeah all souls things will talk and I
wanted to ask a lot of when you will create a little child class the parent classes gets put in the Emerald and parable of and I was wondering when you register all where where is this information internally alone and held it in the class if if you know I don't know and but that was my little you might question that clear or you didn't lose is so if I understood the question is what you were saying is that when they register here yeah a level where is that the concept the that the
the information about the existing is well it I don't know exactly where bottles and it's inside that might made my ABC class so in Python classes are objects right so you can do for example in my ABC delta done their name and you get the name of the class an inside the idea that actually I don't have an answer so I don't I don't know the name of the active attitudes uh when I was investigating biking object found that the year as a very useful financial triangle him circuits or I I tried it in find the that's that's where the right it's OK as enticing I put deer all on on the sequence and I was trying to find like when you register with and will all we can do it later objective figure out very interesting thank you
anyone else if know I've got 1 version of so in back to the question of why a y metaclass and not a decorator have you thought about why just not make it on the register of class method and then inherit instead of having a metaclass mainly well I think it's because you want to um I OK Python you can have multiple inheritance rights it's a house so if uh it's fine for very simple cases but when you try to have too
many uh tyrants and the problem is that you done you know theoretically uh what the methods of solution or the right but uh in practice it's sometimes very complex if you use jungle sometimes we would make since you really don't know who defined that the thing that meant so the problem we need to um yeah inheritance is that you create some very answers 3 and so I think that the point is to avoid pushing father inheritance which is already a bit to abuse the sometimes we forget composition you know so it could be possible here in
in in theory and when i in practice as well that's uh practically leads to with the use of multiple intent inheritance and i'd like they don't to this this way you OK thank you any other questions it's not as tho they had for speaker the thank you