Add materials for Python Metaclasses - #843
Open
realpython-bot wants to merge 1 commit into
Open
realpython-bot wants to merge 1 commit into
realpython-bot wants to merge 1 commit into
Conversation
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is a NEW folder for a tutorial that never had companion code, so it needs an editorial eye as well as a code review — nothing here can be diffed against a previous version of the folder.
What's here
The tutorial demonstrates everything in the interactive interpreter — it contains no named scripts at all, only
pyconblocks. Following the team's instruction, each REPL sequence is collected into a runnable.pyfile that usesprint()so the reader sees the output the article shows. Article order and variable names are preserved, and nothing has been added or "improved".new_style_classes.pytype_and_class.pytype_with_one_argument.pytype()intro)dynamic_class_example_1.pydynamic_class_example_2.pydynamic_class_example_3.pydynamic_class_example_4.pycustom_new_method.py__new__()to a normal class)reassigning_type_new.pycustom_metaclass.pyMeta(type), thenFoo/Bar/Qux)object_factory.pyclass_factory.pysimple_inheritance.pyclass_decorator.pyEach
Example Nfile keeps both halves the article shows — the dynamictype()definition and the equivalentclassstatement — one after the other, because the point of those subsections is that the two are equivalent.dynamic_class_example_2.pyre-createsFoowithtype()at the top, because the article's Example 2 relies on theFooleft over from Example 1 in the same REPL session; the line is commented as such.reassigning_type_new.pywrapstype.__new__ = newintry/except TypeError as ex: print(ex)(the pattern already used inpython-mixins/andpython-enum/) so the file runs to completion while still printing the article's error. It prints the article's message verbatim:cannot set '__new__' attribute of immutable type 'type'.Deliberately not included
The one block in Old-Style vs. New-Style Classes → Old-Style Classes is captured from Python 2.7 (
<type 'instance'>). It cannot run on Python 3 and its output cannot be reproduced in this repo, so it is not shipped as a file. Flagging it here rather than skipping it quietly — if the editorial view is that it should ship (e.g. as a commented-out reference), say so and I'll add it.No
requirements.txtThe draft's
dependenciesfield ispython==3.14— stdlib only, no third-party packages — so this folder ships norequirements.txt, matching siblings such aspython-namespace/,python-isinstance/andpython-double-underscore/.How this was verified
uv venv --python 3.14, CPython 3.14.6) and executed all 14 scripts. Every one exits 0, and the printed output matches the article block for block. The only differences are the object memory addresses indynamic_class_example_1.py(<__main__.Foo object at 0x...>), which are per-run by nature.requirements.txtpins (ruff 0.14.1):uvx ruff@0.14.1 format --check python-metaclasses→ 14 files already formatted;uvx ruff@0.14.1 check python-metaclasses→ All checks passed!git clean -ndandgit status --porcelainare both empty; no venv, cache, or generated output is committed.🤖 Generated with Claude Code