[Python-Dev] Why wont duplicate methods be flagged as error (syntax or anything suitable error) (original) (raw)

Ivan Levkivskyi levkivskyi at gmail.com
Sun Jan 14 04:04:06 EST 2018


On 14 January 2018 at 08:20, Chris Angelico <rosuav at gmail.com> wrote:

On Sun, Jan 14, 2018 at 7:10 PM, joannah nanjekye <nanjekyejoannah at gmail.com> wrote: > Hello, > > Apparently when you implement two methods with the same name: > > def sub(x, y): > print(x -y) > > def sub(x, y): > print(x -y) > > Even with type hints. > > def sub(x: int, y:int) -> int: > return x - y > > def sub(x: float, y:float) -> float: > return 8 > > If you are from another background, you will expect the syntax with type > hints to act as though method overloading but instead last implementation is > always called. If this is the required behavior,then just flag any duplicate > method implementations as syntax errors. > > Is this sort of method name duplication important in any cases? > > Not aimed at criticism, just to understand.

This is not an error in the language for the same reason that any other assignment isn't an error: x = 5 x = 6 But you will find that a number of linters will flag this as a warning. You can configure your editor to constantly run a linter and show you when something's wrong.

For example mypy (and probably also PyCharm) warn about variable/function/class re-definition.

-- Ivan -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20180114/f2f51833/attachment.html>



More information about the Python-Dev mailing list