I have constructed my own class User which has the property userID. I override the __repr__ so that it's the userID string.
In another class I have an array self.users = [] with a couple User instances. For testing purposes I gave them user ID's 1 and 2. I use:
'[%s]' % ','.join(map(str, self.users))
To print the contents of the array, producing:
[1,2]
I'm trying to make an if statement along the lines of:
if "1" in self.users:
print "True"
Above is a simple representation of what I'm trying to achieve. I have tried many approaches and I can't get the program to print true. Does anyone know how to do this?
'1'.strover the list? Normally people do that to avoid the fact thatlist.__str__calls__repr__on each element—but in your case, you've defined__repr__, and you're specifically looking for the__repr__value…self.usersare. We know they have well defined__str__/__repr__, but that's about it. In order to provide a better solution, you should insert aprint self.usersright before your if statement and post that result as an edit to your questionany(user.userID == '1' for user in self.users)?