Suppose I have the class 'Pizza':
Option 1:
class Pizza:
def __init__(self, tools):
self.tools = tools
self.slices = []
def cut(self, slices: int):
self.slices = self.tools.pizza_cutter(slices=slices)
def eat(self, slices: int):
if len(self.slices) => slices:
diff = len(self.slices) - slices
self.slices[:diff]
else:
print('Not enough slices to eat!')
Option 2:
class Pizza:
def __init__(self):
self.slices = []
def cut(self, slices: int, tools):
self.slices = tools.pizza_cutter(slices=slices)
def eat(self, slices: int):
if len(self.slices) => slices:
diff = len(self.slices) - slices
self.slices[:diff]
else:
print('Not enough slices to eat!')
As you can see it either loads tools
in __init__
, or it just receives the tools
instance in the method that actually uses it.
I am wondering, what is the best approach? Consider that there are multiple instances of Pizza
created each loop and that at the end of this loop I cut()
all the Pizza
s
Generally I will determine whether or not to include something in a class by how I would describe it.
When the word "and" comes into play I know that I've put too much into one class and need to separate it out. In this case, the tools are not an inherent part of a pizza. You can have a pizza without the tools to cut it. Pizza might be hard to cut without tools, but that doesn't make it any less of a pizza.
With this in mind, I would recommend keeping tools out of your pizza
At that point though I would probably change the structure of the class to include whether or not the pizza has been cut, then if someone tries to eat it without the pizza being cut they would be notified appropriately