简体   繁体   中英

Python AssertionError creating a class

I have a class User, and a class Theme. The user class can create a Theme, add a Theme to the Theme's dictionary, and should be able to return the dictionary of themes. I'm really new to python so I'm having trouble with the python logic/syntax

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

and my Theme class:

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

I run the test in testUser:

## test createTheme
    theme1 = Theme("theme", 2, 5, 1, 0)
    self.assertEqual(usr1.createTheme("theme", 2, 5, 1, 0), theme1)

but I get - Traceback (most recent call last): File "/Tests/testUser.py", line 52, in test self.assertEqual(usr1.createTheme("theme", 2, 5, 1, 0), theme1) AssertionError: !=

I am not sure what I'm doing wrong, can anyone please help?

(Also, I have the following methods in User, but haven't been able to test them yet since my createTheme doesn't work, but I could use some help to see if there are errors in my logic/syntax:

# 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]

What is happening

When attempting to determine if two objects are equal, say obj1 == obj2 , Python will do the following.

  1. It will first attempt to call obj1.__eq__(obj2) , that is a method defined in the class of obj1 which should determine the logic for equality.

  2. If this method does not exist, or return NotImplemented , then Python falls back on calling obj2.__eq__(obj1) .

  3. If this is still not conclusive, Python will return id(obj1) == id(obj2) , ie it will tell you if both values are the same object in memory.

In your test, Python has to fall back to the third option and your object are two different instances of the class Theme .

What you want to happen

If you expect objects Theme("theme", 2, 5, 1, 0) and usr1.createTheme("theme", 2, 5, 1, 0) to be equal because they have the same attributes, you have to define the Theme.__eq__ method like so.

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)

Note that I am wrapping the attributes in tuples and I then compare the tuples for readability, but you could also compare attributes one by one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM