if object in lst: #do something As far as I can tell, when you execute this statement it is internally checking == between object and every element in lst, which will refer to the __eq__ methods of these two objects. This can have the implication of two distinct objects being "equal", which is usually desired if all of their attributes are the same.However, is there a way to Pythonically achieve a predicate such as in where the underlying equality check is is - i.e. we're actually checking if the two references are to the same object?
3 Réponses :
Utilisez la fonction n'importe quelle fonction code>:
Si vous souhaitez utiliser spécifiquement est code>, utilisez simplement un filtre comme:
L'adhésion 3List à Python est dictée par la méthode __contains__ DUDER. Vous pouvez choisir de remplacer cela pour une implémentation personnalisée si vous souhaitez utiliser la syntaxe normale " sinon, je suggérerais la réponse de kaya3 à utiliser une vérification génératrice. P> p> dans code>" ":