1

我有这个代码:

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)     

在函数add_toys()中,我有返回值new_list。我想在函数中使用该返回值__ str__并将其定义为toys_list. 然而,当我写toys_list = add_toys(self, toys)它说:

add_toys是一个未定义的变量

4

4 回答 4

4

你的add_toys方法不好,你没有使用toys参数,它不应该返回任何东西,它应该像

class Pet:
    # __init__ is OK

    def add_toys(self, *toys):
        for toy in toys:
            if toy not in self.toys:
                self.toys.append(toy)

    def __str__(self):
        if not 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, self.toys)

使用喜欢

p = Pet("Foo")
p.add_toys("ball")
p.add_toys("plate", "frisbee")
print(p) # Foo is a Unknown that has the following toys: ['ball', 'plate', 'frisbee']

你可以直接使用set

class Pet:

    def __init__(self, name, kind="Unknown"):
        self.name = name
        self.kind = kind
        self.toys = set()

    def add_toys(self, *toys):
        self.toys.update(toys)
于 2021-04-10T13:34:04.310 回答
0

问题是未定义的函数。缺少引用自我(每个类成员、字段或方法都需要) toys_list = self.add_toys():.

重新考虑设计

为什么玩具的收藏有两次管理?(a)作为toys可以有重复的字段,(b)作为返回add_toys将隐藏重复并确保唯一元素。

您必须保持两者同步。它们的功能不同,应该应用于不同的用例。

假设:独特的玩具

我也会直接使用set作为字段的类型toys。因为通过这种方式,您可以重用现有的数据结构,从而保证您在add_toys. 此外,添加/更新功能可以方便地修改集合。

改进:字符串构建

此外,您的字符串函数可以重构:

def __str__(self):
    subject = "{} is a {}".format(self.name, self.kind)
    hasToys = "has no toys" if not self.toys else "has the following toys: {}".format(self.toys)
    return "{subject} that {hasToys}".format (subject, hasToys)

它使用三元运算符而不是 if 语句;直接引用(通过self!)新set字段:self.toys 加上一个 3 步构建,以允许单个返回作为最后清晰可见的期望。

于 2021-04-10T14:28:54.727 回答
0

add_toys 中的第一件事是从类级别变量中访问玩具变量,因此删除该参数,第二件事是add_toys(self,toys)语法不正确,您需要像这样使用它self.add_toys()

class Pet(object):
    def __init__(self,name=""):
        self.name = name
        self.kind = "Unknown"
        self.toys = []
    
    def add_toys(self):
        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 = self.add_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)

选择:

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 toys:
            if toy not in new_list:
                new_list.append(toy)
        return new_list
    
    def __str__(self):
        toys_list = self.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)
于 2021-04-10T13:28:50.443 回答
-1

you can't call class method directly, you must call it using self.

like this ..

self.toys_list= self.add_toys(self, toys)
于 2021-04-10T13:28:29.430 回答