The Sororicide Antipattern
glyph.twistedmatrix.com
glyph.twistedmatrix.com
Furthermore, the criticism of inheritance that the article sets up isn't the one that "composition over inheritance" is meant to address. Languages like Java with access modifiers (public/private/protected) don't have the accidental namespace collision issue the writer demonstrates, and yet "composition over inheritance" is a popular concept in those languages, too. The problem with inheritance is that the Liskov substitution principle is very easy to accidentally violate, and those violations can lead to a brittle and inflexible design over time. (See https://en.wikipedia.org/wiki/Circle-ellipse_problem)
Finally, I don't understand the relevance of the "sororicide" metaphor, nor does the article describe anything identifiable as an antipattern.
> Even if, right now, we were to change it to _note2, the maintainer of A could, in any future release of A, add a new _note2 variable which conflicts with something B is using.
This is why Python offers actual namespacing of properties and methods in the form of a double underscore (__). Double underscore prepends an underscore and the class name to the private property name which prevents these sorts of collisions.
class Bprime(object):
def __init__(self, a):
for var in dir(a):
setattr(self, var, getattr(a, var))
> Uh oh. Looks like composition is worse than inheritance.This is not composition.
That's basically the author's point.
> Once I understood what they meant by “composition”, I was even more surprised to find that I agreed with this assertion.
But if the only argument you have against inheritance is "private names may collide," well, like, computer science has you covered, dude.
No, it seems like a Python-based example of one of the well-known problems with inheritance, which applies even in languages like Java and C++ with explicit compiler-enforced visibility modifiers. Difficulty determining which class in a hierarchy is responsible for a particular member is not only a Python problem, or an "only languages without explicit public/private/protected" problem. One reason why composition is favored -- and remember, "composition over inheritance" goes back to the GoF book and earlier! -- is clearing this up.
Of course, blindly "composing" is also bad, which is what this post points out.
I wouldn't be surprised if someone, for example, decided to hijack PHP's `__call()` function to forward method calls to a component.
Rust has the similar Deref anti-pattern: https://github.com/rust-unofficial/patterns/blob/master/anti...
You are taking advantage of this 'collision' in order to allow generic operations to function at higher/lower levels of the system..
Yes, however in python, as others have pointed out here, this unfortunately entails knowing what all the related layers are doing.. so it should be only one tool in the toolchest depending on the problem at hand..
I think the point from the original post is that a collision of the private namespaces of the parent and child class is a bad thing. Hence the calling out of underscore in the variable names to indicate the values should be considered private.
def __init__(self, a):
for var in dir(a):
setattr(self, var, getattr(a, var))
Has anyone ever done such a thing? Why?