I'm new to creating classes and try to solve a problem/exercise.
When I try to instantiate the toolbox, I try to add some tools to it. My approach is to create it as a list, and append new elements to it. It returns a "None" type attribute, and I don't know how to fix it.
class Toolbox:
def __init__(self):
self.toolbox = []
def add_tool(self, tool):
self.toolbox.append(tool)
def remove_tool(self, tool):
self.toolbox.remove(tool)
class Hammers:
color = ""
def __init__(self, color=''):
pass
def hammer_in(self, nail):
pass
def remove(self, nail):
pass
def paint(self, color):
pass
class Screwdrivers:
size = 10
def __init__(self, size=''):
pass
def tighten(self, screw):
pass
def loosen(self, screw):
pass
toolbox = Toolbox()
hammer = Hammers()
screwdriver = Screwdrivers()
toolbox = toolbox.add_tool(hammer)
toolbox = toolbox.add_tool(screwdriver)
Output :
Traceback (most recent call last):
File "C:Create Classes.py", line 133, in <module>
toolbox = toolbox.add_tool(screwdriver)
AttributeError: 'NoneType' object has no attribute 'add_tool'
I'm trying to : Instantiate a toolbox, a screwdriver, and a hammer.
Put the hammer and screwdriver in the toolbox.
Thank you,
toolbox.add_toolreturnsNone. You assign that totoolbox. Hence the error when you try to dotoolbox.add_toolagain. Just don't assign totoolbox