Files
pydantic/docs/examples/mutation.py
T
Samuel Colvin 92d7689271 Immutability part 2 (#53)
* add construct method, fix #48
* adding copy method
* adding pickle support, fix #40
* tweak copy and add fields copy test
* adding docs for immutability, values and copy
* add docs for pickle
2017-06-21 18:15:08 +01:00

29 lines
466 B
Python

from pydantic import BaseModel
class FooBarModel(BaseModel):
a: str
b: dict
class Config:
allow_mutation = False
foobar = FooBarModel(a='hello', b={'apple': 'pear'})
try:
foobar.a = 'different'
except TypeError as e:
print(e)
# > "FooBarModel" is immutable and does not support item assignment
print(foobar.a)
# > hello
print(foobar.b)
# > {'apple': 'pear'}
foobar.b['apple'] = 'grape'
print(foobar.b)
# > {'apple': 'grape'}