I have this code:
class Pet(object):
def __init__(self,name=""):
self.name = name
self.kind = "Unknown"
self.toys = []
def add_toys(self,toys):
new_list = []
for toy in self.toys:
if toy not in new_list:
new_list.append(toy)
return new_list
def __str__(self):
toys_list = add_toys(self,toys)
if self.toys == []:
return "{} is a {} that has no toys".format(self.name,self.kind)
else:
return "{} is a {} that has the following toys: {}".format(self.name,self.kind,toys_list)
In the function add_toys()
I have the return value new_list.
I want to use that return value in the function __ str__
and define it as toys_list
.
However, when I write toys_list = add_toys(self, toys)
it says that:
add_toys
is an undefined variable