How to pythonically have partially-mutually exclusive optional arguments?

后端 未结 7 2250
鱼传尺愫
鱼传尺愫 2021-02-13 18:04

As a simple example, take a class Ellipse that can return its properties such as area A, circumference C, major/minor axis a/b

7条回答
  •  南笙
    南笙 (楼主)
    2021-02-13 18:59

    For the bonus question it's probably sensible (depending on your use case) to calculate on request but remember the computed value if it's been computed before. E.g.

    @property
    def a(self):
        return self._calc_a()
    
    def _calc_a(self):
        if self.a is None:
            self.a = ...?
        return self.a
    

提交回复
热议问题