As of now, I think that statements such as class A(A) should be avoided.
Is there any reason for this being accepted?
Or do you come up with something wrong that this causes?
Yes, you are probably correct that most of the time you should avoid
writing a class that inherits from another class with the same name.
Even though it is perfectly safe if you know what you are doing, and use
super, that sort of name reuse may be confusing.
As for why it is allowed, it isn’t a specific decision to allow it. It
is just a consequence of the way names and objects work in Python.
It is unusual to rebind a class name, but what is common is subclassing
a name from another package:
from foo import A as FooA
class A(FooA):
You can see here that I’m defining an “A” class which subclasses the “A”
from the “foo” package. So I brought in the foo.A name under a different
local name. Also:
import foo
class A(foo.A):
Real world example from my own code:
import mailbox
...
class Maildir(mailbox.Maildir):
''' A faster Maildir interface.
Trust os.listdir, don't fsync, etc.
'''
Anyway, I’m sure you’ve thought of all this. If class A(A) is
confusing or counterintuitive (and I think I would view it the same) do
it more clearly.
There is one key difference here: in Python, x = x * 2 rebinds the namex to a new object, the result of x * 2. In C++, the object named x is assigned the value x * 2
This just to illustrate the point that “the way names and objects work”, to borrow Steven’s phrase, is quite different in different languages.
C++ forbids redefinition of any types, not just this type of inheritance. The error message you pasted isn’t related to inheritance at all, it’s telling you are trying to redefine A. You would get the same error even if the second definition of A didn’t inherit from any other types, or inherited from a type that was not named A.
Yes, it’s practical and easy to read. In contrast, using the same class name (without module name) is very confusing.
Exactly! Thanks, I didn’t notice that point. (If I can say in addition, ‘redefinition in the same scope’)
It is not so simple to compare different languages.