Monday 11 October 2021

Pydantic: Create a model from namedtuple

‘create_model_from_namedtuple’ method takes a namedtuple and return a model.

 

Example

Point = namedtuple('Point', 'x y')
PointM = create_model_from_namedtuple(Point)

create_model_from_namedtuple_1.py

from collections import namedtuple
from pydantic import BaseModel, create_model_from_namedtuple

Point = namedtuple('Point', 'x y')

PointM = create_model_from_namedtuple(Point)

point1 = PointM(x = 1, y = 2)
print(point1)


Output

x=1 y=2

 

 

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment