How does the in operator determine membership? [duplicate]

2024/10/15 5:17:57

How does the in operator work for Python?

In the example below I have two namedtuples that are different objects, but the in operator evaluates to True for an array containing one of the objects.

Is in checking if the array contains the object, or does it check if any object in the array is equivalent? I tried searching for documentation but couldn't find anything specific, just simple examples.

In [3]: a = namedtuple('test', ['t'])                             In [6]: b = a(1)                      In [7]: c = a(1)                      In [8]: b is c                        
Out[8]: False                         In [9]: id(b)                         
Out[9]: 54740808                      In [10]: id(c)                        
Out[10]: 88817352                     In [11]: c in [b]                     
Out[11]: True                         In [12]: b in [c]                     
Out[12]: True                         In [13]: c == b                       
Out[13]: True           
Answer

The in operator is checking the array contains the object which value is equivalent. Here is example:

In [1]: a = {}In [2]: b = {}In [3]: c = [a]In [4]: id(a), id(b), id(c[0])
Out[4]: (4514911128, 4515638640, 4514911128)In [5]: a in c
Out[5]: TrueIn [6]: b in c
Out[6]: True
https://en.xdnf.cn/q/117868.html

Related Q&A

Python Automatically ignore unicode string

Ive been searching to automatically import some files but since Im on Windows i got the unicode error (because of the "C:\Users\..."). Ive been looking to correct this error and found some h…

How to obtain currency rates from this website converter widget python

How can I implement the currency rates on this website and keep the currencies up to date so that i can access them in python from this website and input and output values and currencies types. I need …

Trying to add sums from a csv file in python

I need to add sums of a csv file. The program is a test for a travel reservation system and the file reads like this:availableSTART,reservations,cancellations,availableEND 20,1,0,18I need to subtract r…

Numerical patterns in Python3 [duplicate]

This question already has answers here:How to print without a newline or space(30 answers)Closed 7 years ago.Im fairly new to programming, i have to start learning it for Uni.I have to make a pattern a…

setsockopt before connect for reactor.connectTCP

I have a small python client which needs a setsockopt after create_socket, but before connect. The non-twisted python code is as follows. How can this be expressed in a twisted environment?create_sock…

Manage quotation marks in XPath (lxml)

I want to extract web elements from the table MANUFACTURING AT A GLANCE in the given website. But the name of the row has (single quote). This is interfering with my syntax. How do I overcome this iss…

exception capture in threads and parent

How do you nicely capture exceptions in Python threads?If you have a threaded python app sys.excepthook does not capture errors in children.When a child raises an exception the parent will continue to…

Writing a program that compares 2 numbers in Python [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Run Python3 without activating the virtual environment

My objective is to run Python 3 code on the AWS Lambda Service, which currently only supports Python 2.7. These are the steps I have done.Since I work on a Mac, setup a docker image similar to the AWS …

Matplotlib functions in tkinter

This is my first python project so I understand that this problem may seem a bit stupid. I am trying to create a Mandelbrot renderer. I am piecing code together from tutorials and code that I understan…