Derives: String representation (reference)¶
This page specifies Debug and Display. The derive catalog and the automatic derives are in Derives and traits.
Debug¶
- Provides:
{value:?}formatting. - Provided by: every
model,classandenum; anewtypewhose underlying type implementsDebug. - Behavior: a model or class formats as its type name followed by its fields in declaration order, each as
name: value, in braces. An enum value formats as its variant name, followed by its payloads in parentheses when it has any:Red,Leaf(1),Error(404, "missing"). A newtype formats as its type name followed by its underlying value in parentheses:UserId(42). Astrvalue is quoted. - Dunder: none.
- Requires: every field type implements
Debug(see Automatic derives,INCAN-T0113).
Display¶
- Provides:
{value}formatting,str(value), andprint(value)andprintln(value); satisfies aDisplaybound. - Provided by: a
__str__(self) -> strmethod, declared on the type, inherited from a class it extends, or supplied by an adopted trait; for an enum that declares values, such asenum Level(str), each variant's value;@derive(Display)on amodel,class,enumornewtype; for a type that adoptsErrorand has noDisplayby one of these, itsmessage()(see Displaying an error). - Behavior: the value displays as the string
__str__returns, a value enum's variant as its value, a type that derivesDisplayas its{value:?}structure (see Debug), and anErroradopter without anotherDisplayas the stringmessage()returns. On an enum that declares values,@derive(Display)displays the variant's value. - Dunder:
__str__(self) -> str. - Requires:
@derive(Display)impliesDebug, so on a newtype its underlying type implementsDebug. - Refused: a
model,class,enumornewtypevalue whose type provides noDisplay, in each position above (INCAN-T0103); see Display.@derive(Display)on a type that has a__str__, declared, inherited or supplied by an adopted trait (INCAN-T0001).
Formats¶
Frozen values use the same representation as their ordinary counterparts. FrozenStr is quoted in structured output; FrozenList, FrozenSet and FrozenDict format their elements structurally, including the decimal point of an integral float. bytes and FrozenBytes have no Display form and are refused with INCAN-T0103.
A union value inside structured output formats as its active member value alone.
model Point:
x: int
y: int
model User:
name: str
email: str
def __str__(self) -> str:
return f"{self.name} <{self.email}>"
enum Level(str):
WARN = "warn"
enum Node:
Leaf(int)
Error(int, str)
type UserId = newtype int
@derive(Display)
model Tag:
name: str
def main() -> None:
p = Point(x=10, y=20)
u = User(name="Alice", email="alice@example.com")
leaf = Node.Leaf(1)
error = Node.Error(404, "missing")
user_id = UserId(42)
t = Tag(name="new")
println(f"{p:?} {u:?} {u} {Level.WARN} {t}")
println(f"{leaf:?} {error:?} {user_id:?}")
| Expression | Output |
|---|---|
f"{p:?}" |
Point { x: 10, y: 20 } |
f"{u:?}" |
User { name: "Alice", email: "alice@example.com" } |
f"{u}" |
Alice <alice@example.com> |
f"{Level.WARN}" |
warn |
f"{leaf:?}" |
Leaf(1) |
f"{error:?}" |
Error(404, "missing") |
f"{user_id:?}" |
UserId(42) |
f"{t}" |
Tag { name: "new" } |