Inheritance and name clashes

S

Steven D'Aprano

And you've just defined a circular inheritance tree -- I wouldn't
expect ANY language to handle such... Presuming it doesn't choke on the
parsing. Or a very confusing set of imports where you are
re-using/re-defining a class name itself.

Er what? No, you've misunderstood me, I wasn't trying to imply that Spam
has *itself* as a base class. That would be ... interesting. No, what I
meant was that one of the base classes of Spam (probably in a different
module), happened to also be called Spam.


You don't even need different modules to demonstrate this:
.... __x = "spam"
.... def test(self):
.... assert self.__x == "spam"
........ __x = "ham"
....

Now accidentally add this:

.... __x = "spam spam spam"
....Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in test
AssertionError



Of course if your module is so huge and confused that you don't know what
names you've already used, you have nobody but yourself to blame for
breakage like this.

But suppose you've just imported Ham from another module, and it inherits
from classes which come from some other library, which in turn inherit
from somewhere else, and so for to some arbitrary level of complexity.
How likely is it that you will know the *entire* inheritance tree from
top to bottom before deciding that it's safe to call your subclass Spam?

Of course in practice this is hardly ever a problem. Python tends to use
shallow inheritance trees where it is quite easy to be confident that the
name of your subclass is unique. Python also tends to encourage solutions
other than inheritance, such as delegation, composition, and functional
or procedural solutions that don't require a subclass at all.

Python encourages an attitude that protecting against accidental name
clashes like this is rarely worth it. It's not that name clashes can't
happen, but it's not worth the added complexity to have the language
protect against them. Ordinary testing and debugging techniques can solve
that problem when AND IF it occurs.

In my opinion, it's barely worthwhile to bother with double-underscore
names in the first place, let alone worth worrying about edge cases where
name mangling fails. I merely mentioned this to demonstrate that name
mangling is not a panacea.


(On the other hand, there are frameworks like Plone...
http://www.artima.com/weblogs/viewpost.jsp?thread=246341)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top