I realize that the answer will vary depending upon the situation and purpose of the code. I was writing a class in a module and realized that redefining the operator methods of the class took quite a lot of code. We use inheritance for classes themselves, why not methods? It does, of course, slow it down a little, but it makes the code much more succinct.
Take the following classes for example:
class Ex1:
def __init__(self, v):
self.v = v
def __add__(self, other):
# Many lines of essential code
return self.v + other.v
def __sub__(self, other):
# Many lines of essential code
return self.v - other.v
def __mul__(self, other):
# Many lines of essential code
return self.v * other.v
def __truediv__(self, other):
# Many lines of essential code
return self.v / other.v
and:
import operator as ops
class Ex2:
def __init__(self, v):
self.v = v
def __op__(self, other, op):
if op == '+':
op = ops.add
elif op == '-':
op = ops.sub
elif op == '*':
op = ops.mul
elif op == '/':
op = ops.truediv
# Many lines of essential code
return op(self.v, other.v)
def __add__(self, other):
return self.__op__(other, '+')
def __sub__(self, other):
return self.__op__(other, '-')
def __mul__(self, other):
return self.__op__(other, '*')
def __truediv__(self, other):
return self.__op__(other, '/')
Testing them with the timeit module I get this:
>>> timeit('Ex1(352) + Ex1(139)', 'from __main__ import Ex1')
1.2401711247332514
>>> timeit('Ex1(352) + Ex1(139)', 'from __main__ import Ex1')
1.2278626568422624
>>> timeit('Ex1(352) + Ex1(139)', 'from __main__ import Ex1')
1.2270929157546107
>>>
>>> timeit('Ex2(352) + Ex2(139)', 'from __main__ import Ex2')
1.6781722774976515
>>> timeit('Ex2(352) + Ex2(139)', 'from __main__ import Ex2')
1.6906975044787487
>>> timeit('Ex2(352) + Ex2(139)', 'from __main__ import Ex2')
1.678191572340893
So Ex2
is about 1.36
times slower than Ex1
. But in some (rare) situations if one were to use the "work around" of Ex2
, one would be able to eliminate hundreds of lines of code. But in general, would speed be valued over eliminating lines? Would a professional prefer to sacrifice a little speed to eliminate redundant lines of code?
Edit:
I realized that I can eliminate the if-statements by replacing the op
parameter (from the symbol; eg. '+') with the actual function (eg. ops.add).