Python supports LSP, does it?

Roy Smith roy at panix.com
Wed Aug 10 05:06:42 CEST 2005


Andy Leszczynski <leszczynscyATnospam.yahoo.com.nospam> writes:
> Is not LSP more a quality of the desing of class hierachy rather then 
> language itslef? Comments?

I do agree that LSP is more a class hierarchy thing than a language thing.  
For example, it's easy in Python to write a LSP-violating set of classes:

class base:
    def f(self):
        print "I am base"

class child (base):
    f = "not a function"

Since you can call base.f(), LSP says you should also be able to call 
child.f().  Well, I suppose you can, but most people would not consider 
throwing a TypeError to satisfy the LSP :-).

Mike Meyer <mwm at mired.org> wrote:
> It's not a true statement. Nothing in the language enforces LSP. In
> fact, there's not even a [way?] when a function/method is invoked to make
> sure the type passed in is a subtype of the type you expect

Well, that's not entirely true. I could write:

def func (self, obj):
   assert (isinstance (obj, baseClass))

It's sort of un-pythonic, but the language certainly lets you do it if you 
really want to.



More information about the Python-list mailing list