user.age after validation?from pydantic import BaseModel, Field class User(BaseModel): name: str age: int = Field(default=18, ge=0, le=120) user = User(name='Alice', age=25)
The age field has a default of 18 but since 25 is provided and it meets the constraints (between 0 and 120), the value is set to 25.
email field of type str.Option C correctly imports Optional and sets the default to None. Option C uses valid syntax in Python 3.10+ but lacks a default value, making the field required. Options A and D misuse Optional.
from pydantic import BaseModel class Product(BaseModel): id: int price: float product = Product(id='abc', price=19.99)
id field and the value passed.The id field expects an integer but a string 'abc' is provided. Pydantic raises a ValidationError listing the type mismatch.
item.total_price output?from pydantic import BaseModel class Item(BaseModel): name: str quantity: int price_per_unit: float @property def total_price(self) -> float: return self.quantity * self.price_per_unit item = Item(name='Pen', quantity=3, price_per_unit=1.5) print(item.total_price)
The total_price property multiplies quantity (3) by price_per_unit (1.5), resulting in 4.5.
Pydantic automatically validates nested models recursively. Each nested model's fields are validated according to their definitions without extra manual steps.
