I have a superclass that looks like:
class Channel:
def __init__(self):
# Mutual preparation stuff
self.some_computational_expensive_method()
def run(self, conf, method="tcp"):
if method == "tcp":
return self.run_as_tcp(conf)
elif method == "udp":
return self.run_as_udp(conf)
else:
raise ValueError
And I want to define some child (simplified versions) classes that overrides de run
method to return either run_as_tcp
or run as udp
, like:
class TCPChannel(Channel):
def run(self, conf):
return self.run_as_tcp(conf)
class UDPChannel(Channel):
def run(self, conf):
return self.run_as_udp(conf)
But if I do that (override the method), I am not matching the run
signature of the Channel
class.
Is there a pythonic way to do this?
EDIT: There is a reason to have the superclass. In a previous stage (some_computational_expensive_method
) some operations are performed. So if I want to try both run methods (as_udp and as_tcp) I don't want to create two separate objects (TCPChannel and UDPChannel) and use their own run methods, because that will run the expensive task . Instead, I want to create a Channel object and use run
method twice with different argument.
But if I don't want to have UDP functionalities, I will use the reduced version, ' TCPChannel`.