Differences creating tuples and collections.namedtuples

Gregory Ewing greg.ewing at canterbury.ac.nz
Tue Feb 19 09:27:25 CET 2013


Steven D'Aprano wrote:
> py> class MyDict(dict):
> ...     @classmethod
> ...     def fromkeys(cls, func):
> ...         # Expects a callback function that gets called with no arguments
> ...         # and returns two items, a list of keys and a default value.
> ...         return super(MyDict, cls).fromkeys(*func())

Here you've overridden a method with one having a
different signature. That's not something you'd
normally do, because, being a method, it's likely
to get invoked polymorphically.

Constructors, on the other hand, are usually *not*
invoked polymorphically. Most of the time we know
exactly which constructor we're calling, because we
write the class name explicitly at the point of call.

Consequently, we have a different attitude when it
comes to constructors. We choose not to require LSP
for constructors, because it turns out to be very
useful not to be bound by that constraint.
Practicality beats purity here.

The reason IPython gets into trouble is that it tries
to make a polymorphic call to something that nobody
expects to need to be polymorphic.

-- 
Greg



More information about the Python-list mailing list