This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Duplicate method names should be an error
Type: Stage:
Components: Versions:
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: inglesp, r.david.murray, rhettinger, vstinner
Priority: normal Keywords:

Created on 2016-11-22 20:49 by inglesp, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (8)
msg281513 - (view) Author: Peter Inglesby (inglesp) * Date: 2016-11-22 20:49
It should be an error for a class to define a method twice.

That is, Python should raise an exception when the following code is loaded:

class C:
    def m(self):
        # do something
    def m(self):
        # do something

I have just witnessed a beginner get very confused by this!

(Apologies if this is a duplicate of an existing issue.)
msg281515 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-11-22 20:51
Redefine a method is a common practice indirectly using decorators:

  @staticmethod
  def method(): pass

is like:

  def method(): pass
  method = staticmethod(method)

So you can clarify what do you mean by "redefining"?

Some linters already can such common mistake (very common mistake in unit tests when using copy & paste).
msg281516 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-11-22 21:00
Yes, IMO this is something that needs to go into a linter, not Python itself.  The dynamic nature of the class dictionary is too important to many programs to change it at this point.
msg281518 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-11-22 21:04
Without modifying the language, I guess that it's already technically possible to implement that in Python 3 in an application. Using the __prepare__() method of a metaclass and a custom dict subclass, I think that you can build what you want. I mean that you would have to modify your classes to inherit from a base class (which uses the metaclass) and/or use the metaclass everywhere, to catch such bug...

I'm not sure that it's worth it. You don't want to have to modify your code to catch a single class of kind. Linters exist to check the code without having to modify it.
msg281526 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2016-11-22 22:38
> Yes, IMO this is something that needs to go into a linter,
> not Python itself.

I concur with David Murray.  This would break too many classes and is at odds with the design of Python where code inside a class definition is executed in its own namespace where it has the same rights and privileges as code execed elsewhere.
msg281527 - (view) Author: Peter Inglesby (inglesp) * Date: 2016-11-22 22:41
Victor, I'm not talking about redefining a method, and David, I don't think I'm talking about changing dynamic nature of the class dictionary.

All I'm suggesting is that if some code contains a class whose body defines the same method twice, Python should treat it as an error.  At the moment, as far as I can tell, the first method is ignored.  Is there any situation where this behaviour is useful?

Python already handles a somewhat similar situation by raising a SyntaxError when it encounters a function whose parameter list contains duplicates:

>>> def f(a, a):
...   pass
... 
  File "<stdin>", line 1
SyntaxError: duplicate argument 'a' in function definition
msg281529 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-11-22 22:51
It isn't ignored.  The first definition is entered into the class dictionary, then the second definition replaces it.  That's why Victor talked about redefining a method, because that's what happens.  You can't really disentangle the two cases except by source inspection (a linter).  In the parameter list case, the parser has the information it needs to detect it in hand, and more importantly there is no meaning to "replacing" a parameter, so we can and do generate an error in that case.
msg281530 - (view) Author: Peter Inglesby (inglesp) * Date: 2016-11-22 22:57
OK, I'll defer to your collective decades of experience and wisdom!  Thanks for all you all do for Python.
History
Date User Action Args
2022-04-11 14:58:39adminsetgithub: 72962
2016-11-22 22:57:46inglespsetmessages: + msg281530
2016-11-22 22:51:25r.david.murraysetmessages: + msg281529
2016-11-22 22:41:57inglespsetmessages: + msg281527
2016-11-22 22:38:40rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg281526

resolution: rejected
2016-11-22 21:04:25vstinnersetmessages: + msg281518
2016-11-22 21:00:38r.david.murraysetnosy: + r.david.murray
messages: + msg281516
2016-11-22 20:51:45vstinnersetnosy: + vstinner
messages: + msg281515
2016-11-22 20:49:46inglespcreate