While it is mostly an extension of dict rather than a modification of behaviours already defined in dict, this isn't true for. The dict class is "open for extension, closed for modification" - if you wish to modify some of the prepackaged behaviours of a dict, you're advised to inherit from or collections.UserDict instead.Ĭollections.Counter, however, is a direct subclass of dict. Then, surely this class definition (and all others like it) violates the contract specified by object? object and Foo cannot be used interchangeably, as object accepts exactly 0 parameters to its constructor, while Foo accepts exactly 1.Īs Raymond Hettinger tells us, the Python dictionary is an excellent example of the open/closed principle. With this in mind, how can any python classes that accept a nonzero number of parameters for their constructors be said to comply with the LSP? If I have a class Foo, like so: class Foo: When called, it accepts no arguments and returns a new featureless instance that has no instance attributes and cannot be given any. But the docstring of object includes this line: I'm trying to wrap my head around the Liskov Substitution Principle so that I can write better, more object-oriented code.Īs we know, in Python 3, all custom classes implicitly inherit from object. As such, my knowledge of theory is a little weak. I've taught myself Python over the past year-and-a-bit, and would consider myself an intermediate Python user at this point, but never studied computing at school/university.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |