How are method default arguments overridden in python?
You can change the signatures of overridden methods in any way. Python doesn't care:
class Base:
def foo(self, x, y):
pass
class Deriv(Base):
def foo(self, blah=100):
pass
but if you ask
Is this considered bad style?
The answer is Yes, because it violates an important Liskov substitution principle :
if Deriv extends Base, you should be able to replace all occurrences of Base with Deriv without breaking your program.
In other words, the derived class must fulfill all the contracts provided by the base class. In particular, overridden methods must have the same signatures and similar semantics. Since Python doesn't help you with this, you have to manage it manually using your IDE (Intellij IDEA here):
To answer your specific question about overriding default options, I think the answer is "depends". If the parameter is a parameter that is only used internally and does not affect the observed behavior of the object, there is nothing wrong with changing it:
class Buffer:
def __init__(self, init_size=16):
class BigBuffer(Buffer):
def __init__(self, init_size=1024):
on the other hand, if param significantly affects semantics, it is part of the contract and should not be overridden. For example this code will be confusing
class Test:
def test_equal(self, a, b, fail_if_equal=False):
class MyTest(Test):
def test_equal(self, a, b, fail_if_equal=True):
source to share