from dataclasses import dataclass
@dataclass
class Product:
name: str
count: str
class Products(list):
def __iadd__(self, other):
if not other:
return self
if not isinstance(other, Product):
other = Product(**other)
self.append(other)
return self
class User:
def __init__(self, name=None, phone=None, product=None):
self.name = name
self.phone = phone
self.products = Products()
self.products += product
def __iadd__(self, other):
self.name = other.name or self.name
self.phone = other.phone or self.phone
self.products.extend(other.products)
return self
def __str__(self):
return f'Имя: {self.name}, phone {self.phone}, products: {", ".join(map(str, self.products))}'
user = User('вася', product={'name': 'банан', 'count': '1 шт'})
user += User(phone='3256324235', product={'name': 'не банан', 'count': '1 шт'})
print(user)
Имя: вася, phone 3256324235, products: Product(name='банан', count='1 шт'), Product(name='не банан', count='1 шт')