On this page
Flat overrides and live creation probes๏
Prerequisite: addressing and
lifetimes. A flat override supplies constructor inputs
for the object being resolved. A factory can instead capture configuration at its
registration site. The saved mailer lesson demonstrates both with concrete values.
Choose where configuration belongs๏
The factory returns a mailer configured for its fixed environment. The separately
registered many class accepts host and port through meld(override={...}).
Its assertions compare both resulting configurations.
An override is not a new binding or a global configuration edit. Pair the choice with the intended instance lifetime; repeated reuse and fresh construction are different operations. Follow the complete script's lifetime choices first.
Inspect before constructing๏
has_live_creation(...) asks whether an addressed object already exists.
The live-probe lesson checks False before its first meld and True afterward,
then uses describe_live_creation_status(...) for detail. A diagnostic path can
use these reads without using a meld merely to discover whether something is alive.
Given a spell_id as spell=, both probes answer for exactly that version, also one a
notch has parked or a copy borrowed from a linked conduit, while meld builds only
the selected version.
Continue to deep overrides when the target is inside a dependency graph rather than a parameter of the object you are resolving.
For a registered resolvable=False definition, supply its consumer's value through the same override
API. The supplied object remains your reference; this does not transfer lifecycle ownership. Missing
ordinary required arguments use Python's normal constructor error, including in cached execution.
Existing whole-child overrides still eagerly construct registered children before replacement;
they do not prune the child's constructor inputs. Plan for that existing behavior when replacing a branch.
The workflow in code๏
These are the core steps from the saved example. Use its complete linked script for all class definitions and setup.
1def main() -> None:
2 book = md.Spellbook()
3 book.bind(spell=production_mailer, existence="unique",
4 spellframe="mail", binding_name="mailer")
5 book.bind(spell=SmtpMailer, existence="many",
6 spellframe="mail", binding_name="raw-mailer")
7 conduit = book.conjure()
8
9 mailer = conduit.meld(spellframe="mail", binding_name="mailer")
10 assert (mailer.host, mailer.port) == ("smtp.example.com", 2525)
11 print("factory-configured:", mailer.host, mailer.port)
12
13 overridden = conduit.meld(
14 spellframe="mail", binding_name="raw-mailer",
15 override={"host": "smtp.test.local", "port": 1025},
16 )
17 assert (overridden.host, overridden.port) == ("smtp.test.local", 1025)
18 print("meld-site override:", overridden.host, overridden.port)
Runnable examples๏
Spell override construction โ Intermediate 08
Live creation probe โ Intermediate 18
All intermediate examples ยท Level contents ยท Full contents