|
| 1 | +Arguments number differs from method |
| 2 | +==================================== |
| 3 | + |
| 4 | +Summary |
| 5 | +------- |
| 6 | + |
| 7 | +A child class has implemented an abstract method with a different number of arguments than what is defined in the parent class' method signature. Treat this as a warning. The child class' implementation should match the argument signature defined in the the parent class. |
| 8 | + |
| 9 | +Description |
| 10 | +----------- |
| 11 | + |
| 12 | +If the child class' implementation of the parent class' abstract method contains more arguments than the method signature in the parent class, Python will be able to execute the method. If the child class' implementation has less arguments than what is defined in the parent class' abstract method signature, then Python will not be able to execute the method. Either way, this defeats the purpose of abstract methods. Abstract methods are contracts passed down to inherited classes which signify that the parent class expects its children to implement a method that matches a particular argument signature. When the child class does not match the signature defined in the parent class, it is not fulfilling its part of the inheritance contract. |
| 13 | + |
| 14 | +Examples |
| 15 | +---------- |
| 16 | + |
| 17 | +Child class method contains more arguments than parent class' method signature |
| 18 | +.............................................................................. |
| 19 | + |
| 20 | +In the module below ``Dog`` is a child class of ``Animal``. ``Dog`` is supposed to implement a class called ``print_class_information`` that takes no arguments. However, ``Dog``'s implementation accepts one argument, ``biological_family``. |
| 21 | + |
| 22 | +.. warning:: The code below is an example of an error. Using this code will create bugs in your programs! |
| 23 | + |
| 24 | +.. code:: python |
| 25 | +
|
| 26 | + import abc |
| 27 | +
|
| 28 | + class Animal(object): |
| 29 | + __metaclass__ = abc.ABCMeta |
| 30 | +
|
| 31 | + def __init__(self, number_of_legs): |
| 32 | + self.number_of_legs = number_of_legs |
| 33 | +
|
| 34 | + @abc.abstractmethod |
| 35 | + def print_class_information(self): |
| 36 | + return |
| 37 | +
|
| 38 | + class Dog(Animal): |
| 39 | + def __init__(self): |
| 40 | + Animal.__init__(self, 4) |
| 41 | + def print_class_information(self, biological_family): # signature does not match Animal's |
| 42 | + print "Biological Family: {:s}".format(biological_family) |
| 43 | + print "Class Name: Dog" |
| 44 | +
|
| 45 | + d = Dog() |
| 46 | + d.print_class_information("Canidae") |
| 47 | +
|
| 48 | +Solutions |
| 49 | +--------- |
| 50 | + |
| 51 | +Implement the child class method to match the parent class signature |
| 52 | +.................................................................... |
| 53 | + |
| 54 | +In the modified module below, the ``Dog`` class has removed the second argument from the ``print_class_information()`` method signature. The method now matches the signature defined in ``Animal``. |
| 55 | + |
| 56 | +.. code:: python |
| 57 | +
|
| 58 | + import abc |
| 59 | +
|
| 60 | + class Animal(object): |
| 61 | + __metaclass__ = abc.ABCMeta |
| 62 | +
|
| 63 | + def __init__(self, number_of_legs): |
| 64 | + self.number_of_legs = number_of_legs |
| 65 | +
|
| 66 | + @abc.abstractmethod |
| 67 | + def print_class_information(self): |
| 68 | + return |
| 69 | +
|
| 70 | + class Dog(Animal): |
| 71 | + def __init__(self): |
| 72 | + Animal.__init__(self, 4) |
| 73 | + def print_class_information(self): # signature matches Animal's now |
| 74 | + print "Biological Family: Canidae" |
| 75 | + print "Class Name: Dog" |
| 76 | +
|
| 77 | + d = Dog() |
| 78 | + d.print_class_information() |
| 79 | +
|
| 80 | +References |
| 81 | +---------- |
0 commit comments