I am writing a simple Python class named "Bag":
class Bag:
def __init__(self, items=None):
if self.items == None:
self.items = defaultdict()
else:
temp_dict = defaultdict()
for key in self.items:
temp_dict[key] += 1
self.items = temp_dict
The variable items takes in a list of objects, such as:
['d','a','b','d','c','b','d']
From there, "def __init__(self, items=None)" will either:
- Initialize items as an empty defaultdict, if nothing was passed into items, or
- Initialize the passed in argument, if a list of objects were passed into items.
For example, this should work:
b = Bag()
The absence of an argument should be fine, as items is set, by default, to _None.
However, this always raises an exception(from a script that checks for errors):
*Error: b = Bag() raised exception AttributeError: 'Bag' object has no attribute 'items'
I want to initialize Bag() without putting a passing an argument into items.
Hope everything is clear, tell me if it isn't.
Any ideas or anything wrong with the code?