Overloading (or alternatives) in Python API design

后端 未结 5 1971
半阙折子戏
半阙折子戏 2021-01-03 02:07

I have a large existing program library that currently has a .NET binding, and I\'m thinking about writing a Python binding. The existing API makes extensive use of signatur

5条回答
  •  [愿得一人]
    2021-01-03 02:37

    You could use a dictionary, like so

    Circle({'points':[p1,p2,p3]})
    Circle({'radius':r})
    Circle({'curves':[c1,c2,c3])
    

    And the initializer would say

    def __init__(args):
      if len(args)>1:
        raise SomeError("only pass one of points, radius, curves")
      if 'points' in args: {blah}
      elsif 'radius' in args: {blahblah}
      elsif 'curves' in args: {evenmoreblah}
      else: raise SomeError("same as above")
    

提交回复
热议问题