Skip to content

Commit a1aa6a9

Browse files
committed
Add section on default values
1 parent be3d38e commit a1aa6a9

1 file changed

Lines changed: 132 additions & 2 deletions

File tree

source-code/pydantic.ipynb

Lines changed: 132 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111
{
1212
"cell_type": "code",
13-
"execution_count": 35,
13+
"execution_count": 1,
1414
"id": "b2ba87d0-2447-4a4d-9bae-eb0e86d39ce1",
1515
"metadata": {},
1616
"outputs": [],
@@ -239,6 +239,136 @@
239239
"As you can see, a Python `dataclass` will not perform validation out-of-the-box."
240240
]
241241
},
242+
{
243+
"cell_type": "markdown",
244+
"id": "f9d4e8b1-22b7-4c25-adc0-fb8eb46ed26f",
245+
"metadata": {},
246+
"source": [
247+
"## Default values"
248+
]
249+
},
250+
{
251+
"cell_type": "markdown",
252+
"id": "3b808ae6-0b73-4fb7-b570-593243b15448",
253+
"metadata": {},
254+
"source": [
255+
"Attributes can have default values. For types such as `int` or `str` these are simple constants for non-trivial types you can use `default_factory`."
256+
]
257+
},
258+
{
259+
"cell_type": "code",
260+
"execution_count": 2,
261+
"id": "e00b9f60-5be3-4cea-9dcd-02497911c33e",
262+
"metadata": {},
263+
"outputs": [],
264+
"source": [
265+
"class Agent(BaseModel):\n",
266+
" name: str\n",
267+
" interactive: bool = False\n",
268+
" tools: list[str] = Field(default_factory=list)"
269+
]
270+
},
271+
{
272+
"cell_type": "code",
273+
"execution_count": 7,
274+
"id": "98381d5a-c8ae-4c70-958d-43206747766c",
275+
"metadata": {},
276+
"outputs": [],
277+
"source": [
278+
"agent1 = Agent(name='my_agent')"
279+
]
280+
},
281+
{
282+
"cell_type": "code",
283+
"execution_count": 5,
284+
"id": "c664f054-64fc-4a35-8cd6-77f9429313d4",
285+
"metadata": {},
286+
"outputs": [
287+
{
288+
"data": {
289+
"text/plain": [
290+
"False"
291+
]
292+
},
293+
"execution_count": 5,
294+
"metadata": {},
295+
"output_type": "execute_result"
296+
}
297+
],
298+
"source": [
299+
"agent1.interactive"
300+
]
301+
},
302+
{
303+
"cell_type": "code",
304+
"execution_count": 6,
305+
"id": "f031bf63-9de5-4c96-b153-e510da32f223",
306+
"metadata": {},
307+
"outputs": [
308+
{
309+
"data": {
310+
"text/plain": [
311+
"[]"
312+
]
313+
},
314+
"execution_count": 6,
315+
"metadata": {},
316+
"output_type": "execute_result"
317+
}
318+
],
319+
"source": [
320+
"agent1.tools"
321+
]
322+
},
323+
{
324+
"cell_type": "code",
325+
"execution_count": 9,
326+
"id": "61bf4564-9266-4edd-a55a-3d796daeffa9",
327+
"metadata": {},
328+
"outputs": [],
329+
"source": [
330+
"agent1.tools.append('screwdriver')"
331+
]
332+
},
333+
{
334+
"cell_type": "code",
335+
"execution_count": 8,
336+
"id": "e5b860c1-530b-4cf7-8e79-30806039ce35",
337+
"metadata": {},
338+
"outputs": [],
339+
"source": [
340+
"agent2 = Agent(name='my_other_agent')"
341+
]
342+
},
343+
{
344+
"cell_type": "code",
345+
"execution_count": 10,
346+
"id": "edfcebbe-5a7d-4c1c-b47a-2d8c3ddd54f3",
347+
"metadata": {},
348+
"outputs": [
349+
{
350+
"data": {
351+
"text/plain": [
352+
"[]"
353+
]
354+
},
355+
"execution_count": 10,
356+
"metadata": {},
357+
"output_type": "execute_result"
358+
}
359+
],
360+
"source": [
361+
"agent2.tools"
362+
]
363+
},
364+
{
365+
"cell_type": "markdown",
366+
"id": "094eb08a-1ae8-4d42-9c18-d635ea3fca70",
367+
"metadata": {},
368+
"source": [
369+
"It is clear that using `default_factory` results in the right behavior."
370+
]
371+
},
242372
{
243373
"cell_type": "markdown",
244374
"id": "c31de1d8-2744-48b7-9914-bfeb90cfdac0",
@@ -608,7 +738,7 @@
608738
"name": "python",
609739
"nbconvert_exporter": "python",
610740
"pygments_lexer": "ipython3",
611-
"version": "3.12.7"
741+
"version": "3.12.3"
612742
}
613743
},
614744
"nbformat": 4,

0 commit comments

Comments
 (0)