Рейтинг  

Яндекс.Метрика
Яндекс цитирования
 

   

Статистика  

Пользователи
7
Материалы
592
Кол-во просмотров материалов
2825081
   


@dataclass

Passing default list argument to dataclasses
from dataclasses import dataclass, field
from typing import List

@dataclass
class Pizza():
    ingredients: List = field(default_factory=['dow', 'tomatoes'])  # <- wrong!

Use a lambda function instead:

@dataclass
class Pizza():
    ingredients: List = field(default_factory=lambda: ['dow', 'tomatoes'])


pydantic dataclass

from pydantic.dataclasses import dataclass

@dataclass
class User:
    id: int
    name: str

user = User(id=123, name="James")
d = asdict(user)  # {'id': 123, 'name': 'James'}
user_json = json.dumps(d)
print(user_json)  # '{"id": 123, "name": "James"}'

json_raw = '{"id": 123, "name": "James"}'
user_dict = json.loads(json_raw)
user = User(**user_dict)


   
   

Login Form