-2
import dataclasses

@dataclasses.dataclass
class FlexibilityResult:
    lengths: list[float]
    breadths: list[float]
    heights: list[float]

    volumes: list[float] = dataclasses.field(default_factory=lambda: [])

    def __post_init__(self):
        for length, breadth, height in zip(
            self.lengths, self.breadths, self.heights
        ):
            self.volumes.append(length*breadth*height)

This is running as expected when I run the method locally, but when a Rest GET API hits and the params are passed, the response has the post_init method executed Twice.

When I hit the API with params as (1,2,3), (1,2,3), (1,2,3) as lengths, breadths, heights respectively, the response should have volumes = (1,8,27) in the dataclass, but it gives (1,8,27,1,8,27) For some reason the post_init runs twice.

  • Post code that actually runs and reproduces the problem. It looks like you tried to retype your dataclass definition by hand, or possibly even just made up code and assumed it would be close enough. Trying to instantiate this dataclass produces a `ValueError: too many values to unpack (expected 2)`, not the behavior you described. – user2357112 Sep 01 '23 at 13:06
  • That's becuase of a typo, I've edited now, it shouldn't give the value error now. – Badri Venkatesan Sep 01 '23 at 13:48
  • "That's becuase of a typo" - typos shouldn't even be a possibility, because you should copy-paste your code directly from something you actually ran. Again, you need to post something that **runs** and **reproduces the problem** when run. – user2357112 Sep 01 '23 at 13:51

0 Answers0