Methods with the same name in one class in Python

前端 未结 9 1649
执笔经年
执笔经年 2021-01-30 02:18

How can I declare a few methods with the same name, but with different numbers of parameters or different types in one class?

What must I change in the following class?

9条回答
  •  失恋的感觉
    2021-01-30 02:47

    You can try multimethods in Python:

    http://www.artima.com/weblogs/viewpost.jsp?thread=101605

    But I don't believe multimethod is a way to go. Rather objects that you pass to a method should have common interface. You are trying to achieve method overloading similar to the one in C++, but it is very rarely required in Python. One way to do this is a cascade of ifs using isinstance, but that's ugly.

提交回复
热议问题