Metaclasses in Python

Metaclasses in Python allow you to deeply modify the behaviour of Python classes (in terms of how they’re defined, instantiated, accessed, and more) by replacing the type metaclass that new classes use by default.

Metaclasses in Python: Basic Metaclasses

When type is called with three arguments it behaves as the (meta)class it is, and creates a new instance, ie. it produces a new class/type.

Dummy = type('OtherDummy', (), dict(x=1))
Dummy.class #
Dummy().class.class #

It is possible to subclass type to create an custom metaclass.

class mytype(type):
def init(cls, name, bases, dict):

call the base initializer

type.init(cls, name, bases, dict)
perform custom initialization…
cls.custom_attribute = 2

Now, we have a new custom mytype metaclass which can be used to create classes in the same manner as type.

MyDummy = mytype('MyDummy', (), dict(x=2))
MyDummy.class #
MyDummy().class.class # MyDummy.custom_attribute # 2

When we create a new class using the class keyword the metaclass is by default chosen based on upon the baseclasses.

class Foo(object):
…pass
type(Foo)
type

In the above example the only baseclass is object so our metaclass will be the type of object, which is type. It is possible override the default, however it depends on whether we use Python 2 or Python 3:

Python 2.x Version ≤ 2.7

A special class-level attribute metaclass can be used to specify the metaclass.

class MyDummy(object):
metaclass = mytype
type(MyDummy) #
Python 3.x Version ≥ 3.0

A special metaclass keyword argument specify the metaclass.

class MyDummy(metaclass=mytype):
pass
type(MyDummy) #

Any keyword arguments (except metaclass) in the class declaration will be passed to the metaclass. Thus class MyDummy(metaclass=mytype, x=2) will pass x=2 as a keyword argument to the mytype constructor.

Read this in-depth description of python meta-classes for more details.

Metaclasses in Python: Singletons using metaclasses

A singleton is a pattern that restricts the instantiation of a class to one instance/object. For more info on python singleton design patterns, see here.

class SingletonType(type):
def call(cls, *args, *kwargs): try: return cls.instance except AttributeError: cls.__instance = super(SingletonType, cls).__call(args, **kwargs)
return cls.__instance

Python 2.x Version ≤ 2.7

class MySingleton(object):
metaclass = SingletonType

Python 3.x Version ≥ 3.0

class MySingleton(metaclass=SingletonType):
pass
MySingleton() is MySingleton() # True, only one instantiation occurs

Metaclasses in Python: Using a metaclass

Metaclass syntax

Python 2.x Version ≤ 2.7

class MyClass(object):
metaclass = SomeMetaclass

Python 3.x Version ≥ 3.0

class MyClass(metaclass=SomeMetaclass):
pass

Python 2 and 3 compatibility with six

import six
class MyClass(six.with_metaclass(SomeMetaclass)):
pass

Metaclasses in Python: Introduction to Metaclasses

What is a metaclass?

In Python, everything is an object: integers, strings, lists, even functions and classes themselves are objects. And every object is an instance of a class.

To check the class of an object x, one can call type(x), so:

type(5)
type(str)
type([1, 2, 3])
class C(object):
…pass

type(C)

Most classes in python are instances of type. type itself is also a class. Such classes whose instances are also classes are called metaclasses.

The Simplest Metaclass

OK, so there is already one metaclass in Python: type. Can we create another one?

class SimplestMetaclass(type):
pass
class MyClass(object):
metaclass = SimplestMetaclass

That does not add any functionality, but it is a new metaclass, see that MyClass is now an instance of

SimplestMetaclass:

type(MyClass)

A Metaclass which does Something

A metaclass which does something usually overrides type’s new, to modify some properties of the class to be created, before calling the original new which creates the class:

class AnotherMetaclass(type):
def new(cls, name, parents, dct):
cls is this class
name is the name of the class to be created
parents is the list of the class's parent classes
dct is the list of class's attributes (methods, static variables)
here all of the attributes can be modified before creating the class, e.g.
dct['x'] = 8 # now the class will have a static variable x = 8

return value is the new class. super will take care of that

return super(AnotherMetaclass, cls).new(cls, name, parents, dct)

Custom functionality with metaclasses

Functionality in metaclasses can be changed so that whenever a class is built, a string is printed to standard output, or an exception is thrown. This metaclass will print the name of the class being built.

class VerboseMetaclass(type):
def new(cls, class_name, class_parents, class_dict):
print("Creating class ", class_name)
new_class = super().new(cls, class_name, class_parents, class_dict)
return new_class

You can use the metaclass like so:

class Spam(metaclass=VerboseMetaclass):
def eggs(self):
print("[insert example string here]")
s = Spam()
s.eggs()

The standard output will be:

Creating class Spam

[insert example string here]

The default metaclass

You may have heard that everything in Python is an object. It is true, and all objects have a class:

type(1)
int

The literal 1 is an instance of int. Let’s declare a class:

class Foo(object):
…pass

Now let’s instantiate it:

bar = Foo()

What is the class of bar?

type(bar)
Foo

Nice, bar is an instance of Foo. But what is the class of Foo itself?

type(Foo)
type

Ok, Foo itself is an instance of type. How about type itself?

type(type)
type

So what is a metaclass? For now let’s pretend it is just a fancy name for the class of a class. Takeaways:

Everything is an object in Python, so everything has a class

The class of a class is called a metaclass

The default metaclass is type, and by far it is the most common metaclass

But why should you know about metaclasses? Well, Python itself is quite “hackable”, and the concept of metaclass is important if you are doing advanced stuff like meta-programming or if you want to control how your classes are initialized.

Learn More

Must Read Python Interview Questions

200+ Python Tutorials With Coding Examples

Python Language Basics TutorialPython String Representations of Class Instances
Python For Beginners TutorialPython Debugging Tutorial
Python Data Types TutorialReading and Writing CSV File Using Python
Python Indentation TutorialWriting to CSV in Python from String/List
Python Comments and Documentation TutorialPython Dynamic Code Execution Tutorial
Python Date And Time TutorialPython Code Distributing using Pyinstaller
Python Date Formatting TutorialPython Data Visualization Tutorial
Python Enum TutorialPython Interpreter Tutorial
Python Set TutorialPython Args and Kwargs
Python Mathematical Operators TutorialPython Garbage Collection Tutorial
Python Bitwise Operators TutorialPython Pickle Data Serialisation
Python Bolean Operators TutorialPython Binary Data Tutorial
Python Operator Precedance TutorialPython Idioms Tutorial
Python Variable Scope And Binding TutorialPython Data Serialization Tutorial
Python Conditionals TutorialPython Multiprocessing Tutorial
Python Comparisons TutorialPython Multithreading Tutorial
Python Loops TutorialPython Processes and Threads
Python Arrays TutorialPython Concurrency Tutorial
Python Multidimensional Arrays TutorialPython Parallel Computation Tutorial
Python List TutorialPython Sockets Module Tutorial
Python List Comprehensions TutorialPython Websockets Tutorial
Python List Slicing TutorialSockets Encryption Decryption in Python
Python Grouby() TutorialPython Networking Tutorial
Python Linked Lists TutorialPython http Server Tutorial
Linked List Node TutorialPython Flask Tutorial
Python Filter TutorialIntroduction to Rabbitmq using Amqpstorm Python
Python Heapq TutorialPython Descriptor Tutorial
Python Tuple TutorialPython Tempflile Tutorial
Python Basic Input And Output TutorialInput Subset and Output External Data Files using Pandas in Python
Python Files And Folders I/O TutorialUnzipping Files in Python Tutorial
Python os.path TutorialWorking with Zip Archives in Python
Python Iterables And Iterators Tutorialgzip in Python Tutorial
Python Functions TutorialStack in Python Tutorial
Defining Functions With List Arguments In PythonWorking with Global Interpreter Lock (GIL)
Functional Programming In PythonPython Deployment Tutorial
Partial Functions In PythonPython Logging Tutorial
Decorators Function In PythonPython Server Sent Events Tutorial
Python Classes TutorialPython Web Server Gateway Interface (WSGI)
Python Metaclasses TutorialPython Alternatives to Switch Statement
Python String Formatting TutorialPython Packing and Unpacking Tutorial
Python String Methods TutorialAccessing Python Sourcecode and Bytecode
Using Loops Within Functions In PythonPython Mixins Tutorial
Python Importing Modules TutorialPython Attribute Access Tutorial
Difference Betweeb Module And Package In PythonPython Arcpy Tutorial
Python Math Module TutorialPython Abstract Base Class Tutorial
Python Complex Math TutorialPython Plugin and Extension Classes
Python Collections Module TutorialPython Immutable Datatypes Tutorial
Python Operator Module TutorialPython Incompatibilities Moving from Python 2 to Python 3
Python JSON Module TutorialPython 2to3 Tool Tutorial
Python Sqlite3 Module TutorialNon-Official Python implementations
Python os Module TutorialPython Abstract Syntax Tree
Python Locale Module TutorialPython Unicode and Bytes
Python Itertools Module TutorialPython Serial Communication (pyserial)
Python Asyncio Module TutorialNeo4j and Cypher using Py2Neo
Python Random Module TutorialBasic Curses with Python
Python Functools Module TutorialTemplates in Python
Python dis Module TutorialPython Pillow
Python Base64 Module TutorialPython CLI subcommands with precise help output
Python Queue Module TutorialPython Database Access
Python Deque Module TutorialConnecting Python to SQL Server
Python Webbrowser Module TutorialPython and Excel
Python tkinter TutorialPython Turtle Graphics
Python pyautogui Module TutorialPython Persistence
Python Indexing And Slicing TutorialPython Design Patterns
Python Plotting With Matplotlib TutorialPython hashlib
Python Graph Tool TutorialCreating a Windows Service Using Python
Python Generators TutorialMutable vs Immutable (and Hashable) in Python
Python Reduce TutorialPython configparser
Python Map Function TutorialPython Optical Character Recognition
Python Exponentiation TutorialPython Virtual Environments
Python Searching TutorialPython Virtual Environment – virtualenv
Sorting Minimum And Maximum In PythonPython Virtual environment with virtualenvwrapper
Python Print Function TutorialCreate virtual environment with virtualenvwrapper in windows
Python Regular Expressions Regex TutorialPython sys Tutorial
Copying Data In Python TutorialChemPy – Python package
Python Context Managers (“with” Statement) TutorialPython pygame
Python Name Special Variable TutorialPython pyglet
Checking Path Existence And Permissions In PythonWorking with Audio in Python
Creating Python Packages TutorialPython pyaudio
Usage of pip Module In Python TutorialPython shelve
Python PyPi Package Manager TutorialIoT Programming with Python and Raspberry PI
Parsing Command Line Arguments In Pythonkivy – Cross-platform Python Framework for NUI Development
Python Subprocess Library TutorialPandas Transform
Python setup.py TutorialPython vs. JavaScript
Python Recursion TutorialCall Python from C#
Python Type Hints TutorialPython Writing Extensions
Python Exceptions TutorialPython Lex-Yacc
Raise Custom Exceptions In PythonPython Unit Testing
Python Commonwealth Exceptions TutorialPython py.test
Python urllib TutorialPython Profiling
Web Scraping With Python TutorialPython Speed of Program
Python HTML Parsing TutorialPython Performance Optimization
Manipulating XML In PythonPython Security and Cryptography
Python Requests Post TutorialSecure Shell Connection in Python
Python Distribution TutorialPython Anti Patterns
Python Property Objects TutorialPython Common Pitfalls
Python Overloading TutorialPython Hidden Features
Python Polymorphism TutorialPython For Machine Learning
Python Method Overriding TutorialPython Interview Questions And Answers For Experienced
Python User Defined Methods TutorialPython Coding Interview Questions And Answers
Python Programming Tutorials With Examples

Other Python Tutorials

Leave a Comment