79691598

Date: 2025-07-06 08:39:23
Score: 0.5
Natty:
Report link

I get a solution, i use one build method, but it don't seem so perfect. Because code is not reusable, it limit index of list. Perhaps as  furas said, i need use a list save values, instead of a1, a2...

@dataclass
class Base():
    def __str__(self):
        data = ','.join([str(x) for x in vars(self).values()])
        return data

@dataclass
class B(Base):
    b1: int = 0
    b2: int = 0
    b3: int = 0

@dataclass
class A(Base):
    a1: int = 0
    a2: int = 0
    a3: int = 0
    b: B = None

    def build(self, *arg):
        a_arg = list(arg[0: 3])
        b_arg= arg[3: 6]
        b = B(*b_arg)
        all_arg = a_arg + [b]

        self.__init__(*all_arg)
        return self

num = [1,2,3,4,5,6]
my_data = A().build(*num)
print(my_data) # 1,2,3,4,5,6
Reasons:
  • Blacklisted phrase (0.5): i need
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: Edward