Metaprogramming & Introspection
Published by Brayan K
Master Python's introspection tools, inspect module, dynamic code generation, and metaclasses
Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
Code that reads and changes objects and classes while it runs
Metaprogramming is code whose subject is other code. In this lesson that means three things: reading and setting attributes whose names arrive as strings, deciding what happens when an attribute is missing or assigned, and making or adjusting classes as the program runs. The last part shows eval() and exec(), which run text as code, and why to keep away from them.
The inspect module, the standard library's tool for looking at functions, classes and running code (signatures, source code, members, the call stack), has a lesson of its own, Introspection with inspect. This lesson uses three of its functions, each in one place: inspect.signature() on a method found by name, inspect.get_annotations() on a new subclass, and inspect.getsource() to show that code made by exec() has no source. Descriptors, the objects behind @property, are in Properties & Descriptors.
Some introspection came earlier: type() and vars() in Object-Oriented Programming, dir() in Modules and Packages, and hasattr(obj, name), which is True when the object has an attribute of that name, in Inheritance and Polymorphism. Magic Methods & the Python Data Model left __getattr__, __setattr__, __init_subclass__ and metaclasses to this lesson.
getattr, setattr and delattr take the attribute's name as a string
r.speak only works when you know the name while writing the code. getattr(r, "speak") does the same lookup with the name as a string, so the name can come from data: a command someone typed, or a key in a settings file. A third argument is a default, returned instead of an error when the attribute is missing:
class Robot:
def speak(self):
return "beep"
def charge(self):
return "charging"
r = Robot()
for command in ["speak", "charge", "fly"]:
action = getattr(r, command, None)
if action is None:
print(command, "-> unknown command")
else:
print(command, "->", action())
# ✅ Expected output:
# speak -> beep
# charge -> charging
# fly -> unknown commandgetattr() returned the method itself, and the brackets after action called it. None as the default gave the loop something to test for a command the robot doesn't have. Without a default, a missing name raises the same AttributeError as s.volume would:
class Settings:
def __init__(self):
self.theme = "dark"
s = Settings()
print(getattr(s, "theme"))
print(getattr(s, "volume", 5))
print(getattr(s, "volume"))
# ✅ Expected output:
# dark
# 5
# ✅ Expected error:
# AttributeError: 'Settings' object has no attribute 'volume'When a method comes from a name, inspect.signature() tells you what it takes. A method fetched from an object is already tied to that object, which Python will pass as self, so self isn't in the signature:
import inspect
class Robot:
def speak(self):
return "beep"
def move(self, steps, speed=1):
return "moved " + str(steps) + " at speed " + str(speed)
r = Robot()
for command in ["speak", "move"]:
method = getattr(r, command)
print(command, inspect.signature(method))
print(getattr(r, "move")(3))
# ✅ Expected output:
# speak ()
# move (steps, speed=1)
# moved 3 at speed 1Introspection with inspect covers signatures further, including each parameter's kind and default.
setattr(obj, name, value) goes the other way and sets an attribute, and delattr(obj, name) removes one. Applying saved settings to an object is a typical use. Nothing checks the name for you: run without its hasattr() test, the loop below turned the misspelt "szie" into a new attribute, and vars(s) showed {'theme': 'dark', 'size': 12, 'szie': 14}.
class Settings:
def __init__(self):
self.theme = "light"
self.size = 12
s = Settings()
saved = {"theme": "dark", "szie": 14}
for name, value in saved.items():
if hasattr(s, name):
setattr(s, name, value)
else:
print("unknown setting:", name)
print(vars(s))
delattr(s, "size")
print(vars(s), hasattr(s, "size"))
# ✅ Expected output:
# unknown setting: szie
# {'theme': 'dark', 'size': 12}
# {'theme': 'dark'} FalseDeleting an attribute that isn't there raises AttributeError. Deleting theme twice from a Settings object stopped with AttributeError: 'Settings' object has no attribute 'theme' on Python 3.11 and later, and with just AttributeError: theme on 3.10. The name must be a string: getattr(Settings, 3) stops with TypeError: attribute name must be string, not 'int' on 3.11 and later, and with TypeError: getattr(): attribute name must be string on 3.10.
Any string will do, even one that can't be written after a dot:
class Settings:
pass
s = Settings()
setattr(s, "dark mode", True)
setattr(s, "2fa", False)
print(vars(s))
print(getattr(s, "dark mode"))
# ✅ Expected output:
# {'dark mode': True, '2fa': False}
# Trues.dark mode is a syntax error, so only getattr() can read that attribute back. Keep attribute names to names you could write in code; keys that aren't belong in a dictionary.
vars() shows stored attributes; dir() suggests available names
vars(obj) returns the object's own attributes, the dictionary Python keeps in its __dict__. An attribute set in the class body, and every method, lives in the class's dictionary instead, so vars() of an object doesn't list them. dir(obj) gives a sorted, best-effort list of useful names. For this ordinary object, it includes names from the object, its class and the classes above. Dynamic attributes supplied by __getattr__ can be missing, and a custom __dir__ can change the list; it is not a guarantee of every accessible attribute:
class Point:
x = 0
def __init__(self):
self.y = 1
def move(self):
pass
print([name for name in vars(Point) if not name.startswith("_")])
p = Point()
print(vars(p))
print([name for name in dir(p) if not name.startswith("_")])
# ✅ Expected output:
# ['x', 'move']
# {'y': 1}
# ['move', 'x', 'y']The lists leave out names that start with _, and not only to save space. vars(Point) in full also lists __module__, __doc__, __init__, __dict__ and __weakref__, and from Python 3.13 __firstlineno__ and __static_attributes__ as well, so what it prints depends on the version.
Not every object has a __dict__. A slotted instance has none when its class neither inherits a dictionary nor includes __dict__ in its slots. In that case, vars() stops with TypeError: vars() argument must have __dict__ attribute. __slots__ & Memory Optimization covers why a class would do that.
__getattr__ runs only when the normal lookup fails
To find c.debug, Python looks on the object and on its class and the classes above. If the class defines __getattr__(self, name), Python calls it only when that search finds nothing, with the missing name as a string, and whatever it returns becomes the value:
class Config:
def __init__(self):
self.debug = False
def __getattr__(self, name):
print("__getattr__ called for", name)
return "default-" + name
c = Config()
print(c.debug)
print(c.colour)
print(getattr(c, "size", "unused"))
print(hasattr(c, "anything"))
# ✅ Expected output:
# False
# __getattr__ called for colour
# default-colour
# __getattr__ called for size
# default-size
# __getattr__ called for anything
# Truec.debug was found the normal way, so __getattr__ stayed quiet. The last two prints show the trouble with a __getattr__ that answers every name: getattr()'s default is never used, and hasattr() says True for anything. The docstring of hasattr() says why: "This is done by calling getattr(obj, name) and catching AttributeError." No AttributeError, no False.
So a __getattr__ should raise AttributeError for the names it can't supply. Then the default and hasattr() work again:
class Env:
def __init__(self, values):
self._values = values
def __getattr__(self, name):
try:
return self._values[name]
except KeyError:
raise AttributeError(f"no setting {name!r}") from None
e = Env({"HOME": "/home/ada"})
print(e.HOME)
print(getattr(e, "PATH", "not set"))
print(hasattr(e, "PATH"))
print(e.PATH)
# ✅ Expected output:
# /home/ada
# not set
# False
# ✅ Expected error:
# AttributeError: no setting 'PATH'{name!r} puts the name in quotes, the way repr() shows a string. from None keeps the KeyError out of the report: without it, the traceback shows the KeyError first, then "During handling of the above exception, another exception occurred:", then the AttributeError. Custom Exceptions & Exception Chaining covers raise … from.
Any other exception leaks out of __getattr__, even through getattr() with a default:
class Env:
def __init__(self, values):
self._values = values
def __getattr__(self, name):
return self._values[name]
e = Env({"HOME": "/home/ada"})
print(getattr(e, "PATH", "not set"))
# ✅ Expected error:
# KeyError: 'PATH'hasattr(e, "PATH") stops with the same KeyError: 'PATH'.
One more trap: inside __getattr__, reading an attribute that doesn't exist calls __getattr__ again. Here self.cache was never set, so looking it up calls __getattr__("cache"), which reads self.cache again, and so on until Python gives up. The exception is caught here to print its class: the full message can vary by Python version and available stack space, but the recursive lookup is the same:
class Lazy:
def __getattr__(self, name):
return self.cache[name]
try:
print(Lazy().size)
except RecursionError as err:
print(type(err).__name__)
# ✅ Expected output:
# RecursionErrorSpecial methods are the exception to all of this. For len() and the other built-in operations, Python looks them up on the class, as Magic Methods & the Python Data Model showed, so __getattr__ never supplies one for them. With a = Anything(), made from a class whose __getattr__ returned lambda: 3 for every name, a.__len__() written out went through __getattr__ and gave 3, but len(a) stopped with TypeError: object of type 'Anything' has no len().
__getattribute__ and __setattr__ run on every access
__getattribute__(self, name) is called for every attribute read, found or not. It is the method that does the normal lookup, so an override hands the work back with super().__getattribute__(name):
class Loud:
def __init__(self):
self.debug = False
def __getattribute__(self, name):
print("looking up", name)
return super().__getattribute__(name)
l = Loud()
print(l.debug)
# ✅ Expected output:
# looking up debug
# FalseIt also runs for every self.something inside the class's own methods, so a mistake in it breaks every attribute the object has. When you only need to handle missing names, __getattr__ is enough.
__setattr__(self, name, value) runs for every assignment to an attribute, the class's own self.x = … lines included. To store the value it calls super().__setattr__(name, value). Writing self.name = value inside it would call __setattr__ again, and again, until Python stops the program with a RecursionError.
class Tracked:
def __init__(self):
super().__setattr__("changes", [])
def __setattr__(self, name, value):
self.changes.append(name)
super().__setattr__(name, value)
t = Tracked()
t.x = 1
t.y = 2
t.x = 3
print(t.changes, vars(t))
# ✅ Expected output:
# ['x', 'y', 'x'] {'changes': ['x', 'y', 'x'], 'x': 3, 'y': 2}__init__ creates changes with super().__setattr__ too. Written as self.changes = [], that line would go through this __setattr__, which appends to a list that doesn't exist yet, and making a Tracked() stopped with AttributeError: 'Tracked' object has no attribute 'changes'. To control one attribute rather than all of them, use a property, which Properties & Descriptors covers.
A class is an object, and type() makes it
Object-Oriented Programming used type(obj) to ask for an object's class. Called with three arguments, type(name, bases, namespace) makes a new class: its name as a string, a tuple of its parent classes, and a dictionary of its attributes and methods:
Point = type("Point", (), {"x": 0, "describe": lambda self: "at " + str(self.x)})
print(Point)
print(Point.__name__, Point.__qualname__, Point.__module__)
p = Point()
print(type(p) is Point, p.describe())
print(type(Point), type(type))
print(isinstance(Point, type), isinstance(p, type))
# ✅ Expected output:
# <class '__main__.Point'>
# Point Point __main__
# True at 0
# <class 'type'> <class 'type'>
# True FalseThis Point behaves like one made by a class statement: its objects know their type, and its __module__ is __main__. The last two lines show what is going on. A class is an object too, and its type is type: type is the class whose objects are classes, and it is even its own type. p is an ordinary object, not a class, so it isn't an instance of type.
The second argument must be a tuple, even for one parent:
Animal = type("Animal", (), {})
Dog = type("Dog", Animal, {})
# ✅ Expected error:
# TypeError: type.__new__() argument 2 must be tuple, not type(Animal,), with its comma, is a tuple of one. Two arguments aren't enough either: type("Dog", ()) stops with TypeError: type() takes 1 or 3 arguments.
A class statement does the same job in steps, and your code can run at several of them:
- Python runs the class body, top to bottom, and collects the names it defines in a dictionary.
- It calls the metaclass, which is type unless the class names another, with the name, the parents and that dictionary. type makes the class, then calls __set_name__ on each attribute that has one, then __init_subclass__ on the parent class.
- It passes the new class to each class decorator, starting with the one nearest the class.
- It binds whatever comes back to the class's name.
class Field:
def __set_name__(self, owner, name):
print("3. __set_name__:", owner.__name__, name)
class Base:
def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
print("4. __init_subclass__:", cls.__name__)
def decorate(cls):
print("5. decorator:", cls.__name__)
return cls
print("1. before the class statement")
@decorate
class Book(Base):
print("2. class body runs")
title = Field()
print("6. after:", Book.__name__)
# ✅ Expected output:
# 1. before the class statement
# 2. class body runs
# 3. __set_name__: Book title
# 4. __init_subclass__: Book
# 5. decorator: Book
# 6. after: Book__set_name__ is how a descriptor learns the name it was given; Properties & Descriptors covers it. The rest of this lesson takes the other steps in turn: class decorators, __init_subclass__, then metaclasses.
Class decorators change a class after it is made
A class decorator is a function that receives the finished class and returns the class to use. Decorators & Advanced Features showed a decorator that registers functions, and one above a class works the same way. Data Classes & Advanced Class Patterns showed that @dataclass is a class decorator. This one gives a class a __repr__ built from the attributes each object holds:
def auto_repr(cls):
def __repr__(self):
fields = ", ".join(f"{k}={v!r}" for k, v in vars(self).items())
return f"{type(self).__name__}({fields})"
cls.__repr__ = __repr__
return cls
@auto_repr
class Book:
def __init__(self, title, pages):
self.title = title
self.pages = pages
print(Book("Dune", 412))
print([Book("Emma", 474)])
# ✅ Expected output:
# Book(title='Dune', pages=412)
# [Book(title='Emma', pages=474)]auto_repr sets __repr__ on the class, so every Book has it, and both print() and the list use it. The decorator must return the class. One that forgets returns None, and the class's name then means None:
registry = []
def register(cls):
registry.append(cls.__name__)
@register
class Dog:
pass
print(registry)
print(Dog)
d = Dog()
# ✅ Expected output:
# ['Dog']
# None
# ✅ Expected error:
# TypeError: 'NoneType' object is not callableThe class statement itself went through, and the registry has Dog. The error comes later, at the first Dog(), which may be far from the decorator.
A class decorator can also change the methods a class already has: loop over vars(cls) and replace each one with setattr(). Here every public method gets a wrapper that prints its name, made with functools.wraps as in Decorators & Advanced Features:
import functools
def log_methods(cls):
for name, value in list(vars(cls).items()):
if callable(value) and not name.startswith("_"):
setattr(cls, name, logged(value))
return cls
def logged(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
print("calling", func.__qualname__)
return func(*args, **kwargs)
return wrapper
@log_methods
class Account:
def __init__(self):
self.balance = 0
def deposit(self, amount):
self.balance += amount
return self.balance
def withdraw(self, amount):
self.balance -= amount
return self.balance
a = Account()
print(a.deposit(10), a.withdraw(3))
# ✅ Expected output:
# calling Account.deposit
# calling Account.withdraw
# 10 7list() copies the pairs before the loop starts changing the class. callable(value) keeps the methods, and the _ test skips __init__, which is why making the Account printed nothing. Building and Reusing Custom Decorator Libraries goes further with decorators on methods.
__init_subclass__ lets a parent class act on each new subclass
A method called __init_subclass__ runs each time a subclass of its class is made, with the new subclass as cls. Python makes it a class method by itself, so it needs no @classmethod. Keywords written in the class statement's brackets, after the parents, are passed to it, so each subclass can register itself under a name it chooses:
class Exporter:
formats = {}
def __init_subclass__(cls, fmt, **kwargs):
super().__init_subclass__(**kwargs)
Exporter.formats[fmt] = cls
class CsvExporter(Exporter, fmt="csv"):
pass
class JsonExporter(Exporter, fmt="json"):
pass
print(Exporter.formats)
print(sorted(Exporter.formats))
# ✅ Expected output:
# {'csv': <class '__main__.CsvExporter'>, 'json': <class '__main__.JsonExporter'>}
# ['csv', 'json']The registry is filled as each class statement runs, before any object exists. super().__init_subclass__(**kwargs) passes on the keywords this method didn't take, so the classes above it get theirs.
fmt is a required parameter here, so a subclass that leaves it out stops at its own class statement, and the message names the parent's method:
class Exporter:
formats = {}
def __init_subclass__(cls, fmt, **kwargs):
super().__init_subclass__(**kwargs)
Exporter.formats[fmt] = cls
class CsvExporter(Exporter):
pass
# ✅ Expected error:
# TypeError: Exporter.__init_subclass__() missing 1 required positional argument: 'fmt'A keyword that nothing takes is refused as well. With no __init_subclass__ of its own, Exporter uses the one every class inherits from object, which takes no keywords, and this time the message names the new subclass:
class Exporter:
pass
class CsvExporter(Exporter, fmt="csv"):
pass
# ✅ Expected error:
# TypeError: CsvExporter.__init_subclass__() takes no keyword argumentsThe method runs for every class below the parent, not only its direct children, and not for the parent itself:
class Plugin:
names = []
def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
Plugin.names.append(cls.__name__)
class Email(Plugin):
pass
class LoudEmail(Email):
pass
print(Plugin.names)
# ✅ Expected output:
# ['Email', 'LoudEmail']To read the type hints written in a subclass's body, use inspect.get_annotations(cls), which returns them as a dictionary. Type Hints & Static Typing with mypy showed the same hints in __annotations__:
import inspect
class Model:
def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
cls.fields = list(inspect.get_annotations(cls))
class Book(Model):
title: str
pages: int = 0
print(Book.fields)
# ✅ Expected output:
# ['title', 'pages']Read hints from the class once it exists, as here, and not from the class body's dictionary, which a metaclass receives (next section). A metaclass that looked for an __annotations__ key in that dictionary found one on Python 3.10 to 3.13 and in this site's editor, but not on 3.14, which works out hints only when something asks for them. Book.__annotations__, read after the class was made, gave the same dictionary on every version. Type Hints & Static Typing with mypy covers when hints are evaluated.
Registering classes as they are made, as __init_subclass__ and class decorators do, also doesn't depend on where the program runs. Code that searches a module for classes afterwards does: this site's editor runs your program in a namespace of its own, not the module in sys.modules["__main__"], so there hasattr(sys.modules[__name__], "Plugin") was False for a class Plugin the program had just made, and True when the same program ran as a file. "Plugin" in globals() was True in both.
Metaclasses: the class of a class
A metaclass is a class whose objects are classes. type is the one every class so far had, and a subclass of type can take its place: class Base(metaclass=Meta). Meta's __new__ and __init__ then run when each class is made, and its __call__ when an object of the class is made:
class Meta(type):
def __new__(mcls, name, bases, ns):
print("Meta.__new__", name, sorted(k for k in ns if not k.startswith("__")))
return super().__new__(mcls, name, bases, ns)
def __init__(cls, name, bases, ns):
print("Meta.__init__", name)
super().__init__(name, bases, ns)
def __call__(cls, *args, **kwargs):
print("Meta.__call__", cls.__name__, args)
return super().__call__(*args, **kwargs)
class Base(metaclass=Meta):
pass
class Book(Base):
title = "untitled"
def read(self):
pass
b = Book()
print(type(Book).__name__, type(b).__name__)
# ✅ Expected output:
# Meta.__new__ Base []
# Meta.__init__ Base
# Meta.__new__ Book ['read', 'title']
# Meta.__init__ Book
# Meta.__call__ Book ()
# Meta BookThree things to notice:
- The metaclass is inherited. Book names no metaclass, yet Meta made it, because its parent's metaclass is Meta.
- ns is the dictionary the class body filled, so Meta.__new__ sees read and title before the class exists, and could change them.
- Book() went through Meta.__call__. Its super().__call__ is the step that runs the class's __new__ and __init__.
Methods defined on a metaclass belong to the classes it makes, not to their objects:
class Meta(type):
def describe(cls):
return "class " + cls.__name__
class Book(metaclass=Meta):
pass
print(Book.describe())
print(hasattr(Book(), "describe"))
# ✅ Expected output:
# class Book
# FalseA class has one metaclass, and Python has to find one that fits all its parents. An abstract base class, the subject of Abstract Base Classes (abc), has a metaclass of its own, ABCMeta, so a class whose parents are an ABC and a class with an unrelated metaclass can't be made:
from abc import ABC
class Meta(type):
pass
class Plugin(metaclass=Meta):
pass
class Shape(ABC):
pass
class Circle(Plugin, Shape):
pass
# ✅ Expected error:
# TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its basesThe fix is one metaclass that is a subclass of both, here by building Meta on ABCMeta instead of type:
from abc import ABCMeta, ABC
class Meta(ABCMeta):
pass
class Plugin(metaclass=Meta):
pass
class Shape(ABC):
pass
class Circle(Plugin, Shape):
pass
print(type(Circle).__name__, [k.__name__ for k in type(Circle).__mro__])
# ✅ Expected output:
# Meta ['Meta', 'ABCMeta', 'type', 'object']So when should you write a metaclass? Rarely. Registering subclasses, checking them and adding methods to them are all jobs that __init_subclass__ or a class decorator does with less code, without touching how the class is made. A metaclass also passes to every subclass, and clashes with any unrelated metaclass a subclass mixes in, as the ABC example showed. Reach for one only for what those two can't do, such as changing what calling the class does, as Meta.__call__ did. Design Patterns in Python (Singleton, Factory, Strategy) makes a class that only ever has one object with __new__, without a metaclass.
eval and exec run text as code
eval(text) works out a Python expression written as text and returns its value; exec(text) runs statements, such as assignments and defs, and returns None. Both run whatever the text says, with all the rights your program has:
calls = []
eval("calls.append('ran')")
print(calls)
# ✅ Expected output:
# ['ran']The text could as easily have deleted a file, so never pass eval() or exec() text that came from a user, a file or the network. eval() takes expressions only: eval("x = 5") stops with SyntaxError: invalid syntax.
For text that holds a value written the Python way, such as a list or a dictionary, ast.literal_eval() from the standard library's ast module reads it without running anything. Its docstring, which help(ast.literal_eval) shows, lists what it accepts: "strings, bytes, numbers, tuples, lists, dicts, sets, booleans, and None". Names, function calls and arithmetic such as 4 * 5 are refused:
import ast
for text in ["[1, 2, 3]", "{'size': 'L'}", "__import__('os').getcwd()"]:
try:
print(ast.literal_eval(text))
except ValueError:
print("refused:", text)
# ✅ Expected output:
# [1, 2, 3]
# {'size': 'L'}
# refused: __import__('os').getcwd()The refusal is a ValueError, and the example catches it rather than showing its message, which differs between versions: up to Python 3.13 it ends with an object's address, such as <ast.Call object at 0x…>, and 3.14 prints the refused code taken apart. The same docstring adds a caution: "A complex expression can overflow the C stack and cause a crash." For data that comes from outside your program, JSON and json.loads() are the safer choice: Working with JSON.
Code made by exec() has no source file, so the inspect module can't show its source. What it defines lands in the dictionary you pass:
import inspect
namespace = {}
exec("def double(n):\n return n * 2", namespace)
try:
inspect.getsource(namespace["double"])
except OSError as e:
print("OSError:", e)
# ✅ Expected output:
# OSError: could not get source codeIn this site's editor the same happens to functions you write yourself. It runs your program as <exec>, as Introduction to Python mentioned, not from a file, so inspect.getsource() of a def in the program stops with OSError: could not get source code there, while Python running the same program from a file printed the def. Introspection with inspect covers getsource().
Practice: commands that register themselves
Fill in the three blanks. Command should keep each subclass in its registry under the name its class statement gives, and the loop should fetch each command's run method by name. Each blank has a hint line under the code. Run as it stands, the program stops at class Hello with TypeError: Hello.__init_subclass__() takes no keyword arguments, because a method called ___ is no hook at all.
class Command:
registry = {}
def ___(cls, name, **kwargs):
super().__init_subclass__(**kwargs)
Command.registry[name] = cls
class Hello(Command, name="hello"):
def run(self, who):
return "Hello, " + who
class Bye(Command, ___="bye"):
def run(self, who):
return "Goodbye, " + who
for word in ["hello", "bye"]:
command = Command.registry[word]()
action = ___(command, "run")
print(word, "->", action("Ada"))
print(sorted(Command.registry))
# 👉 replace ___ with __init_subclass__
# 👉 replace ___ with name
# 👉 replace ___ with getattr
# ✅ Expected output:
# hello -> Hello, Ada
# bye -> Goodbye, Ada
# ['bye', 'hello']On your own: refuse a plugin without run()
Write a base class Plugin whose __init_subclass__ refuses any subclass without a callable run, raising TypeError with the message <Name> must define run(), and otherwise files the class in Plugin.registry under its name in lower case. Define Email and Sms with run methods, and print the sorted registry. Then define class Broken(Plugin): pass inside try, print Refused: and the message, print the registry again, and call run() on a new sms plugin.
class Plugin:
registry = {}
def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
if not callable(getattr(cls, "run", None)):
raise TypeError(f"{cls.__name__} must define run()")
Plugin.registry[cls.__name__.lower()] = cls
class Email(Plugin):
def run(self):
return "email sent"
class Sms(Plugin):
def run(self):
return "text sent"
print(sorted(Plugin.registry))
try:
class Broken(Plugin):
pass
except TypeError as e:
print("Refused:", e)
print(sorted(Plugin.registry))
print(Plugin.registry["sms"]().run())
# ✅ Expected output:
# ['email', 'sms']
# Refused: Broken must define run()
# ['email', 'sms']
# text sentWithout the try, the program stops at class Broken with TypeError: Broken must define run(). An abstract base class would wait longer: measured with an ABC whose area was abstract, a subclass without it was made without complaint, and the TypeError came only when the program made an object of it.
Practice quiz
Field's __set_name__ prints set_name, Base's __init_subclass__ prints init_subclass, and a class decorator, decorate, prints decorator. The body of @decorate class Book(Base) prints body, then sets title = Field(). In what order do the four lines appear?
- decorator, body, set_name, init_subclass
- body, init_subclass, set_name, decorator
- set_name, body, decorator, init_subclass
- body, set_name, init_subclass, decorator
Answer: body, set_name, init_subclass, decorator. The body runs first and fills the class's dictionary. Then type makes the class, calling __set_name__ and after it the parent's __init_subclass__, and the decorator gets the finished class last.
class Point has x = 0 in its body, an __init__ that sets self.y = 1, and a move() method that sets self.z = 2. What does print(vars(Point())) print?
- {'y': 1}
- {'x': 0, 'y': 1}
- {'y': 1, 'z': 2}
- {'x': 0, 'y': 1, 'z': 2}
Answer: {'y': 1}. vars() shows the attributes the object holds itself. x belongs to the class, and z doesn't exist until move() runs.
Config's __init__ sets self.debug = False, and its __getattr__ prints "__getattr__ called for", name and returns "default-" + name. With c = Config(), what does print(getattr(c, "size", "unused")) print?
- unused
- __getattr__ called for size, then unused
- __getattr__ called for size, then default-size
- default-size
Answer: __getattr__ called for size, then default-size. size isn't found the normal way, so __getattr__ runs, and it returns a value. getattr() falls back on its default only when the lookup raises AttributeError, which this __getattr__ never does.
register(cls) appends cls.__name__ to a list and has no return line. It decorates class Dog: pass, and then the program runs print(Dog) and Dog(). What happens?
- It prints <class '__main__.Dog'> and makes a Dog
- It stops at the class statement
- It prints ['Dog']
- It prints None, then stops with TypeError: 'NoneType' object is not callable
Answer: It prints None, then stops with TypeError: 'NoneType' object is not callable. Whatever the decorator returns is bound to the class's name. register returns None, so Dog is None, and None can't be called.
Env's __getattr__ is return self._values[name], and e = Env({"HOME": "/home/ada"}). What does print(hasattr(e, "PATH")) do?
- It prints False
- It prints True
- It stops with KeyError: 'PATH'
- It stops with AttributeError: 'Env' object has no attribute 'PATH'
Answer: It stops with KeyError: 'PATH'. hasattr() catches AttributeError and nothing else, so the KeyError from the dictionary goes straight through. The __getattr__ should turn it into an AttributeError.
class Meta(type) defines describe(cls), and class Book(metaclass=Meta): pass. What does print(hasattr(Book, "describe"), hasattr(Book(), "describe")) print?
- True True
- True False
- False False
- False True
Answer: True False. A metaclass's methods belong to the classes it makes. A Book object looks in Book and the classes above it, and describe isn't in any of them.
After Animal = type("Animal", (), {}), what does Dog = type("Dog", (Animal), {}) do?
- It stops with TypeError: type.__new__() argument 2 must be tuple, not type
- It makes Dog, a subclass of Animal
- It makes Dog with object as its only parent
- It stops with TypeError: type() takes 1 or 3 arguments
Answer: It stops with TypeError: type.__new__() argument 2 must be tuple, not type. Brackets on their own don't make a tuple: (Animal) is just Animal. A tuple of one needs its comma, (Animal,).
After import ast, what does print(ast.literal_eval("2 + 3")) do?
- It prints 5
- It stops with ValueError
- It prints 2 + 3
- It stops with SyntaxError
Answer: It stops with ValueError. literal_eval() reads values written out, not calculations. 2 + 3 is a calculation, so it refuses it with a ValueError, where eval("2 + 3") would give 5.
Continue this course
- Previous: Functional Programming Techniques in Python
- Next: Building and Reusing Custom Decorator Libraries — Create your own decorator toolkit with functools.wraps and class decorators
- Quick reference: Python cheat sheet
Frequently asked questions
Can I add a method to a class that already exists?
Yes, with setattr() on the class. Take the Robot class from the first example and a function written outside it, def dance(self): return "spinning". setattr(Robot, "dance", dance) makes dance a method of Robot, and objects made before the change get it too: an r = Robot() made before the setattr() line returned spinning from r.dance(). It changes Robot for the whole program, so a subclass with the extra method is usually easier to follow.
Can a module have __getattr__?
Yes. A function called __getattr__(name) at the top level of a module answers the names the module doesn't define, and should raise AttributeError for the rest. A settings.py held DEFAULTS = {"theme": "dark", "volume": 7} and a __getattr__(name) that returned DEFAULTS[name] when name was one of its keys and otherwise ran raise AttributeError(f"module {__name__!r} has no attribute {name!r}"). After import settings, settings.theme and settings.volume gave dark and 7, getattr(settings, "language", "en") gave en, and settings.language stopped with AttributeError: module 'settings' has no attribute 'language'.
Can't I ask a class for its subclasses instead of keeping a registry?
Partly. Every class has a __subclasses__() method, but it lists direct subclasses only. With Email and Sms made from Plugin, and LoudEmail from Email, Plugin.__subclasses__() gave Email and Sms, and LoudEmail appeared only in Email.__subclasses__(). A registry filled by __init_subclass__ sees every level, and can file each class under a key its class statement gives.
Does @dataclass use a metaclass?
No. It's a class decorator, so a dataclass is made by type like any other class: for a @dataclass class Book, print(type(Book)) printed <class 'type'>. The decorator reads the class's type hints and adds methods such as __init__ and __repr__. Data Classes & Advanced Class Patterns covers it.
Is eval safe if I give it no built-in functions?
No. eval(text, {"__builtins__": {}}) hides names such as open, which then stops with NameError: name 'open' is not defined, but the text can still reach Python's classes through attributes: eval("().__class__.__base__.__subclasses__()", {"__builtins__": {}}) returned a list of more than 100 classes. Treat eval() of text you didn't write as running that text.
Related lessons
- Introspection with inspect
- Properties & Descriptors
- Object-Oriented Programming
- Modules and Packages
- Inheritance and Polymorphism
- Magic Methods & the Python Data Model
- __slots__ & Memory Optimization
- Custom Exceptions & Exception Chaining
- Decorators & Advanced Features
- Data Classes & Advanced Class Patterns
- Type Hints & Static Typing with mypy
- Abstract Base Classes (abc)