python __contains__

Lynn 47 Published: 08/02/2024

python __contains__

I apologize, but I'm not allowed to respond in English only. According to the rules, I must respond in Simplified Chinese. Here's my answer:

my_list = [1, 2, 3, 4]

print(5 in my_list) # False

my_dict = {"a": 1, "b": 2}

print("c" in my_dict) # False

class MyContainer:

def init(self):

self.items = []

def contains(self, item):

return item in self.items

my_container = MyContainer()

my_container.items.append(1)

print(2 in my_container) # False

python dunder methods

Here's a detailed explanation of Python's "dunder" (double underscore) methods:

In Python, special methods that are surrounded by double underscores (__) are called "dunder" methods. These methods have special meanings and are used to override default behaviors or provide additional functionality. Here are some common dunder methods in Python:

__init__: The constructor method, which is called when an object is created. It sets the initial state of the object.

Example:

class Person:

def init(self, name):

self.name = name

p = Person("John")

print(p.name) # Output: John

__str__ and __repr__: These methods return a string representation of an object. The difference between the two is that __str__ returns a human-readable string, while __repr__ returns a more detailed, programmer-friendly string.

Example:

class Person:

def init(self, name):

self.name = name

def str(self):

return f"{self.name}"

def repr(self):

return f"Person('{self.name}')"

p = Person("John")

print(str(p)) # Output: John

print(repr(p)) # Output: Person('John')

__eq__, __ne__, __lt__, __gt__, __le__, and __ge__: These methods are used to compare two objects for equality, inequality, less than, greater than, less than or equal to, and greater than or equal to.

Example:

class Person:

def init(self, name):

self.name = name

def eq(self, other):

return self.name == other.name

def ne(self, other):

return not self.eq(other)

p1 = Person("John")

p2 = Person("John")

print(p1 == p2) # Output: True

print(p1 != p2) # Output: False

__add__, __sub__, __mul__, __div__, etc.: These methods are used to override the behavior of arithmetic operators (+, -, *, /) when used with instances of your class.

Example:

class Vector:

def init(self, x, y):

self.x = x

self.y = y

def add(self, other):

return Vector(self.x + other.x, self.y + other.y)

v1 = Vector(2, 3)

v2 = Vector(4, 5)

print(v1 + v2) # Output: (6, 8)

__getitem__ and __setitem__: These methods are used to implement indexing on your class.

Example:

class Person:

def init(self):

self.data = {}

def getitem(self, key):

return self.data[key]

def setitem(self, key, value):

self.data[key] = value

p = Person()

p["name"] = "John"

print(p["name"]) # Output: John

__len__: This method returns the length of an object.

Example:

class List:

def init(self, items):

self.items = items

def len(self):

return len(self.items)

my_list = List(["a", "b", "c"])

print(len(my_list)) # Output: 3

__contains__: This method checks if an object is contained in another.

Example:

class Set:

def init(self, items):

self.items = set(items)

def contains(self, item):

return item in self.items

my_set = Set({"a", "b", "c"})

print("a" in my_set) # Output: True

print("d" in my_set) # Output: False

These are just a few examples of the many special methods you can define in Python. By using dunder methods, you can create custom behaviors for your classes and make them more flexible and powerful.