Python断言错误创建类

Python断言错误创建类,python,Python,我有一个类用户和一个类主题。用户类可以创建主题,将主题添加到主题的字典中,并且应该能够返回主题字典。我对python非常陌生,所以在python逻辑/语法方面遇到了问题 class User: def __init__(self, name): self.themes = {} def createTheme(self, name, themeType, numWorkouts, themeID, timesUsed): newTheme = T

我有一个类用户和一个类主题。用户类可以创建主题,将主题添加到主题的字典中,并且应该能够返回主题字典。我对python非常陌生,所以在python逻辑/语法方面遇到了问题

class User:
    def __init__(self, name):
        self.themes = {}

    def createTheme(self, name, themeType, numWorkouts, themeID, timesUsed):
        newTheme = Theme(name, themeType, numWorkouts, themeID, timesUsed)
        return newTheme
还有我的主题课:

class Theme:
    def __init__(self, name, themeType, numWorkouts, themeID, timesUsed):
        #themeType: 1 = genre, 2 = artist, 3 = song
        self.name = name
        self.themeType = themeType
        self.numWorkouts = numWorkouts
        self.themeID = themeID
        self.timesUsed = timesUsed
我在testUser中运行测试:

## test createTheme
    theme1 = Theme("theme", 2, 5, 1, 0)
    self.assertEqual(usr1.createTheme("theme", 2, 5, 1, 0), theme1)
但我得到- 回溯(最近一次呼叫最后一次): 文件“/Tests/testUser.py”,第52行,在测试中 self.assertEqual(usr1.createTheme(“theme”,2,5,1,0),theme1) 断言者错误:!=

我不知道我做错了什么,有人能帮我吗

(另外,我在User中有以下方法,但由于我的createTheme不起作用,因此还无法测试它们,但我可以使用一些帮助来查看我的逻辑/语法中是否存在错误:

# returns dict
# def getThemes(self):
#     return self.themes
#
# def addTheme(self, themeID, theme):
#     if theme not in self.themes:
#         themes[themeID] = theme
#
# def removeTheme(self, _theme):
#     if _theme.timesUsed == _theme.numWorkouts:
#         del themes[_theme.themeID]
发生了什么事 当试图确定两个对象是否相等时,比如说
obj1==obj2
,Python将执行以下操作

  • 它将首先尝试调用
    obj1.\uuuueq(obj2)
    ,这是一种方法 在
    obj1
    类中定义,该类应确定 平等

  • 如果此方法不存在,或返回
    NotImplemented
    ,则 Python转而调用
    obj2.\uuuueq\uuj1(obj1)

  • 如果这仍然不是决定性的,Python将返回
    id(obj1)==id(obj2)
    , i、 它会告诉你两个值在内存中是否是同一个对象

  • 在测试中,Python必须退回到第三个选项,并且对象是类
    主题的两个不同实例

    你想发生什么 如果您希望对象
    Theme(“Theme”,2,5,1,0)
    usr1.createTheme(“Theme”,2,5,1,0)
    相等,因为它们具有相同的属性,那么您必须像这样定义
    主题

    class Theme:
        def __init__(self, name, themeType, numWorkouts, themeID, timesUsed):
            #themeType: 1 = genre, 2 = artist, 3 = song
            self.name = name
            self.themeType = themeType
            self.numWorkouts = numWorkouts
            self.themeID = themeID
            self.timesUsed = timesUsed
    
        def __eq__(self, other)
            # You can implement the logic for equality here
            return (self.name, self.themeType, self.numWorkouts, self.themeID) ==\
                   (other.name, other.themeType, other.numWorkouts, other.themeID)
    
    请注意,我将属性包装在元组中,然后比较元组的可读性,但您也可以逐个比较属性。

    发生了什么 当试图确定两个对象是否相等时,比如说
    obj1==obj2
    ,Python将执行以下操作

  • 它将首先尝试调用
    obj1.\uuuueq(obj2)
    ,这是一种方法 在
    obj1
    类中定义,该类应确定 平等

  • 如果此方法不存在,或返回
    NotImplemented
    ,则 Python转而调用
    obj2.\uuuueq\uuj1(obj1)

  • 如果这仍然不是决定性的,Python将返回
    id(obj1)==id(obj2)
    , i、 它会告诉你两个值在内存中是否是同一个对象

  • 在测试中,Python必须退回到第三个选项,并且对象是类
    主题的两个不同实例

    你想发生什么 如果您希望对象
    Theme(“Theme”,2,5,1,0)
    usr1.createTheme(“Theme”,2,5,1,0)
    相等,因为它们具有相同的属性,那么您必须像这样定义
    主题

    class Theme:
        def __init__(self, name, themeType, numWorkouts, themeID, timesUsed):
            #themeType: 1 = genre, 2 = artist, 3 = song
            self.name = name
            self.themeType = themeType
            self.numWorkouts = numWorkouts
            self.themeID = themeID
            self.timesUsed = timesUsed
    
        def __eq__(self, other)
            # You can implement the logic for equality here
            return (self.name, self.themeType, self.numWorkouts, self.themeID) ==\
                   (other.name, other.themeType, other.numWorkouts, other.themeID)
    

    请注意,我将属性包装在元组中,然后比较元组的可读性,但您也可以逐个比较属性。

    我应该如何正确测试createTheme方法并将其与正确的结果进行比较?您的测试返回正确的答案。您的方法似乎是错误的,因此这是一个好的测试。这是什么你到底想要什么行为?我想createTheme只是基于输入创建一个新主题,它就是这样做的。你的测试不会因为没有创建一个新实例而失败。它会因为创建了一个新实例而失败。实际上,你为什么期望你的测试为真,对象不相等?我建议你问一个新问题。Stackoverflow不仅是一个帮助用户的平台,而且只是为了收集和组织答案以备将来使用。因此,创建另一个问题将是正确的做法。干杯!我应该如何正确测试createTheme方法并将其与正确的结果进行比较?您的测试返回正确的答案。您的方法似乎是错误的,因此是错误的一个很好的测试。你到底想要什么行为?我想createTheme只是基于输入创建一个新的主题。它就是这样做的。你的测试不会因为没有创建新实例而失败。它失败的原因正是因为创建了一个新实例。实际上,你为什么期望你的测试为真,对象不相等?我建议你你问一个新问题。Stackoverflow不仅仅是一个帮助用户的平台,而且只是为了收集和组织答案供将来使用。因此,创建另一个问题将是正确的做法。干杯!