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

Chris Angelico rosuav at gmail.com
Sun Jan 14 03:20:57 EST 2018


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.

ChrisA


More information about the Python-Dev mailing list