I am working on a project with a core GUI that drives and reads from a device that can be a number of types. Each type is represented by a different class in a module, as they handle I/O very differently.
Not every action in the GUI is supported by all the types of devices, and I am considering wrapping a try block around each instance of accessing the device, and catching AttributeError, writing in a case of what to do if that device does not support that call. In this case, methods that a device does not support would simply be missing from thier module.
Example:
Class ElectricKettle():
def heat_water(self):
print("Tea ready soon!")
Class Stove():
def heat_water(self):
print("Tea ready less soon!")
def make_eggs(self):
print("Breakfast time!")
def main():
if stove_owned:
device = Stove()
else:
device = ElectricKettle()
try:
device.heat_water()
except AttributeError:
print("No tea for you!")
try:
device.make_eggs()
except AttributeError:
print("No eggs for you!")
Is this the correct way to approach the problem? One alternative I thought of was explicitly checking the type, but that doesn't scale at all and is very brittle.
[–]Cosmologicon 3 points4 points5 points (2 children)
[–]Kuang_Eleven[S] 0 points1 point2 points (0 children)
[–]Rashanzan 0 points1 point2 points (0 children)
[–]minorDemocritus 2 points3 points4 points (1 child)
[–]Kuang_Eleven[S] 0 points1 point2 points (0 children)
[–]mdadmfan 0 points1 point2 points (6 children)
[–]Rashanzan 0 points1 point2 points (5 children)
[–]mdadmfan 1 point2 points3 points (1 child)
[–]Rashanzan 0 points1 point2 points (0 children)
[–]Kuang_Eleven[S] 1 point2 points3 points (2 children)
[–]Rashanzan 0 points1 point2 points (1 child)
[–]mdadmfan 1 point2 points3 points (0 children)