0

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?

John Bale
  • 433
  • 7
  • 16
  • 1
    is the value in self.users an int? cause you're checking for the string `'1'`. – kreativitea Nov 28 '12 at 22:14
  • He's mapping the str constructor over the list. That's not the problem. – Wes Nov 28 '12 at 22:15
  • Out of curiosity, why are you mapping `str` over the list? Normally people do that to avoid the fact that `list.__str__` calls `__repr__` on each element—but in your case, you've defined `__repr__`, and you're specifically looking for the `__repr__` value… – abarnert Nov 28 '12 at 22:17
  • 2
    We're really just guessing what the elements in `self.users` are. We know they have well defined `__str__`/`__repr__`, but that's about it. In order to provide a better solution, you should insert a `print self.users` right before your if statement and post that result as an edit to your question – mgilson Nov 28 '12 at 22:17
  • 1
    Is there a reason you need to search by string representation instead of the actual member variable? Why not just `any(user.userID == '1' for user in self.users)`? – abarnert Nov 28 '12 at 22:21

3 Answers3

3

Do you want:

if any("1" in str(x) for x in self.users):
   print "True"

probably better is:

if any("1" == x.userID for x in self.users):
   print True

if I'm reading your post correctly.


Finally, depending on the API for User, you could use the ID in an equality test:

class User(object):
    def __eq__(self,other):
       return self.userID == other
   #other methods here

Now you can use __contains__ (in) on your list as you were trying to do initially:

if "1" in self.users:
   print "True"
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • As I said to Blender, given that he's defined `__repr__` rather than `__str__`, I'd use `repr(x)` here. – abarnert Nov 28 '12 at 22:15
  • @abarnert -- Maybe. `__str__` calls `__repr__` by default though... (and I think that `str` is what should really be used here. I would contend that OP should re-think the decision to override `__repr__` in this way) – mgilson Nov 28 '12 at 22:18
  • Yes, assuming he didn't define that as well (or doesn't do so later). As Blender points out, it's probably better to change his `__repr__` to `__str__` in the first place. But either way, I think it makes sense to search for the one you made sure to define… – abarnert Nov 28 '12 at 22:20
2

"1" in self.users checks to see if the string "1" is in the list self.users. You're trying to check if there exists a user whose string representation is "1", so you could try something like this:

if '1' in map(str, self.users):
    print 'True

Also, I'd change __repr__ to __str__ in your class definition.

__repr__ usually returns a string that can be eval()uated back into the original object, while __str__ returns a human-readable representation of the object. You're doing the latter, so I suggest you use __str__ instead of __repr__.

I'd read through this question as well: Difference between __str__ and __repr__ in Python

Community
  • 1
  • 1
Blender
  • 289,723
  • 53
  • 439
  • 496
  • Given that he's defined `__repr__` rather than `__str__`, I'd use `repr` rather than `str` here. – abarnert Nov 28 '12 at 22:15
  • `itertools.imap` is better here as it is more accommodating of short-cicuiting – inspectorG4dget Nov 28 '12 at 22:16
  • Yes, I think that's a more reasonable fix in general. And in particular, he chose to map `str` rather than `repr` over the list, which implies that he actually wants this to be the `str` representation anyway. – abarnert Nov 28 '12 at 22:18
0

From your problem definition, it sounds like you're trying to find if you have any users whose userID attribute is '1'.

The fact that your __repr__ function happens to use the userID as the representation does mean that's the best way to search for it. Why not just this?

if any(user.userID == '1' for user in self.users):
    print "True"

If you really do need to search for the string representation, of course, use the answer posted by mgilson or Blender (and figure out which of str and repr you want). But if it's not necessary, don't do it.

abarnert
  • 354,177
  • 51
  • 601
  • 671