Reputation: 667
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,
Upvotes: 0
Views: 797
Reputation: 198
As Arseniy says, your problem comes from reassigning toolbox to a new object:
First, a Toolbox object is assigned to the toolbox
reference
toolbox = Toolbox()
Then, the result of toolbox.add_tool, which is the object None
is assigned to the toolbox
reference, thus toolbox
now no longer points to a instantiated Toolbox
object:
toolbox = toolbox.add_tool(hammer)
So, what you should do instead is simply remove the re-assignment:
toolbox.add_tool(hammer)
toolbox.add_tool(screwdriver)
And this should result in:
>>> print (toolbox.toolbox)
[<__main__.Hammers object at 0x...>, <__main__.Screwdrivers object at 0x...>]
Upvotes: 1
Reputation: 42758
add_tool
and remove_tool
return implicitly None
. You should not assign this return value to toolbox
.
You are using class-Attributes wrongly. color
should be a instance attribute.
The default value of size
is wrong, since size is not a empty string.
The class Hammers
represent one Hammer
. Why can you paint with a hammer?
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 Hammer:
def __init__(self, color=''):
self.color = color
def hammer_in(self, nail):
pass
def remove(self, nail):
pass
def paint(self, color):
pass
class Screwdriver:
def __init__(self, size=10):
self.size = size
def tighten(self, screw):
pass
def loosen(self, screw):
pass
toolbox = Toolbox()
hammer = Hammer()
screwdriver = Screwdriver()
toolbox.add_tool(hammer)
toolbox.add_tool(screwdriver)
Upvotes: 0
Reputation: 690
add_tool return nothing (None), and you assign it to toolbox
toolbox = toolbox.add_tool(hammer) <--- problem here
toolbox = toolbox.add_tool(screwdriver)
So, toolbox become None and 'NoneType' object has no attribute 'add_tool'
Upvotes: 1