Does overriding an instance method with a class method violate LSP?


This Content is from Stack Overflow. Question asked by hayden0729

I have a class structure that looks something like this:

class A():

    def getSize(self):
        return int()
class B(A):

    def __init__(self):
        self.size = 1

    def getSize(self):
        return self.size

class C(A):

    size = 2

    def getSize(cls):
        return cls.size

The real calculation in getSize() is more than just storing a variable, but the point is that for B, it varies between instances, while for C, it’s the same every time. Because of this, it’d be nice if I could make getSize() a class method for C, which would let me do C.getSize() rather than needing an instance. However, if I make it a class method like this, pylint complains with an arguments-differ message, which makes me think it might be a flawed approach. You can still use the method on an instance (i.e., C().getSize()), so I’m not really sure what the issue is. Is there a violation of LSP here? Is there a better way I could accomplish this, or should I just ignore the warning?


This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?