I am trying to create a pydantic class with Immutable class fields (not instance fields).
Here is my base code:
from pydantic import BaseModel
class ImmutableModel(BaseModel):
_name: str = "My Name"
_age: int = 25
ImmutableModel._age = 26
print("Class variables:")
print(f"Name: {ImmutableModel._name}")
print(f"Age: {ImmutableModel._age}")
Output:
Class variables:
Name: My Name
Age: 26
I tried using the Config class inside my ImmutableModel to make fields immutable. But it seems like it only works for instance class fields.
class Config:
allow_mutation = False
FYI, I use Python 3.6.13 and pydantic==1.9.2
Initially, I tried to achieve creating immutable
ClassandInstanceusing pydantic module.Now I were able to manage it using native method itself. Since this was completely defined by me and immutable, its fine to have no validation.
All the above code raises AttributeError.