Reputation: 139
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]
Upvotes: 1
Views: 107
Reputation: 22324
When attempting to determine if two objects are equal, say obj1 == obj2
, Python will do the following.
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.
If this method does not exist, or return NotImplemented
, then
Python falls back on calling obj2.__eq__(obj1)
.
If this is still not conclusive, Python will return id(obj1) == id(obj2)
,
i.e. 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
.
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.
Upvotes: 4