0

I'm trying to check if a list that is contained within a class has an element and the 'in' key word is not working. Is there another way of doing this. I've typed an example of what I'm asking about. Please ignore spacing etc ,it's just an example. My problem is in checking if the list tools[] contains an element.

Class Player():
     def __init__(self):
           tools= []
player = Player()

player.tools.append('x')

####right here, is there another way of checking if the list contains the elements #####
If 'x' in player.tools:
    pass
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
  • 1
    `in` works fine. Post some real (not pseudo) code if you need help figuring out why it doesn't work for you. – Aran-Fey Apr 08 '19 at 19:31
  • 1
    In what way is the `in` operator "not working"? – blhsing Apr 08 '19 at 19:31
  • You need to provide a [mcve]. Your current code *won't even compile*, because of a couple of syntax errors. But assuming those weren't there, and a couple other fixes (`tools` being an actual instance variable) this should work fine. So again, please provide a reproducible example of the problem you are trying to solve. To check if a `list` object contains another object, you would almost always use `in` – juanpa.arrivillaga Apr 08 '19 at 19:31
  • There is no list in your class. `tools` is local in the constructor and not part of the class or instance. – Klaus D. Apr 08 '19 at 19:33
  • 1
    @juanpa.arrivillagaI I believe what Linda posted is a perfect example of a question for this site. The post asks a very specific question, contains code for what was attempted so far, and explains what Linda thought would happen compared to what is happening. I think it would have been faster to just explain how using self makes a variable an attribute of the class. – Grant Williams Apr 08 '19 at 19:38
  • Duplicate of https://stackoverflow.com/questions/7571635/fastest-way-to-check-if-a-value-exist-in-a-list – Salvatore Apr 08 '19 at 19:39
  • No one here is answering the question. Are there any other ways of checking if an element is in a list that is within a class without using the in key word – Linda Scoon Apr 08 '19 at 19:41
  • 1
    @LindaScoon I answered the "in keyword is not working" part because it actually should have. I'm not sure OP requested a comparison like the one in the """duplicate""" question. EDIT : you actually are OP. ok my bad – MaximGi Apr 08 '19 at 19:44
  • @GrantWilliams no, it is absolutely not. Questions requiring debugging help require a [mcve]. The OP's example *does not even compile*, and we can only speculate as to why it isn't working, because we do not have a reproducible example. Indeed, that is what we see here: a bunch of speculation of how the OP's code *might not be working*. No where is it described *what isn't happening*. We have no idea if the problem is a lack of using an instance attribute, since the code would never get to that point, and we don't have a description of how the code is failing. – juanpa.arrivillaga Apr 08 '19 at 20:02
  • @LindaScoon why do you not want to use `in`? That is the *correct* way, the alternative is to essentially manually reproduce what the `in` operator is doing, and it will fail just the same. – juanpa.arrivillaga Apr 08 '19 at 20:03
  • @juanpa.arrivillaga their question is asking how to do something in python. How could you possibly post a verifiable and complete solution to something you dont know how to do? Linda posted an attempt at a solution that works in other places in python, but clearly didnt know what an attribute of a class was. – Grant Williams Apr 08 '19 at 20:18
  • @GrantWilliams you aren't supposed to post a *reproducible solution* solution, but a *reproducible example of the problem you are encountering*. The OP;'s attempt *does not work in other places in Python because the OPs attempt doesn't even compile due to syntax errors*. This would be fine if the question was "why does this code produce a SyntaxError", or if they showed an `AttributeError` and asked why, then that would be a [mcve]. Otherwise, *all we have to go on is pure speculation*. If our opinions differ on this, that's fine, that is why SA works based on community voting. – juanpa.arrivillaga Apr 08 '19 at 21:02

1 Answers1

1

There's an error in your class instantiation (which is corrected below) :

Class Player():
     def __init__(self):
           self.tools= []
player = Player()

Instance attributes are designated as "self.attribute". Otherwise, you just declared a local variable in init()'s scope.

To answer your question fully, you should then test like so :

if 'x' in player.tools:
    pass

After assigning :

player.tools.append('x')

OR if you are inside a method of your class :

self.tools.append('x')

You should also have a look at built-in methods such as getattr(), setattr() and hasattr() for further use.

MaximGi
  • 543
  • 3
  • 12