Skip to Content
Advanced Python 3 Programming Techniques
book

Advanced Python 3 Programming Techniques

by Mark Summerfield
February 2009
Intermediate to advanced
68 pages
1h 51m
English
Addison-Wesley Professional
Content preview from Advanced Python 3 Programming Techniques

Chapter 15. Metaclasses

A metaclass is to a class what a class is to an instance; that is, a metaclass is used to create classes, just as classes are used to create instances. And just as we can ask whether an instance belongs to a class by using isinstance(), we can ask whether a class object (such as dict, int, or SortedList) inherits another class using issubclass().

The simplest use of metaclasses is to make custom classes fit into Python’s standard ABC hierarchy. For example, to make SortedList a collections. Sequence, instead of inheriting the ABC (as we showed earlier), we can simply register the SortedList as a collections.Sequence:

class SortedList:    ...collections.Sequence.register(SortedList)

After the class is defined ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python Network Programming Techniques

Python Network Programming Techniques

Marcel Neidinger
Scientific Computing with Python 3

Scientific Computing with Python 3

Claus Führer, Claus Fuhrer, Jan Erik Solem, Olivier Verdier

Publisher Resources

ISBN: 9780321637727