#10 Dunder Methods|Magic Methods|Special methods in python|python for beginners|OOP

Publicado el: 22 mayo 2024
en el canal de: pythonbuzz
187
like

#python dunder methods#python#dunder methods#dunder methods in python#python magic methods#python class dunder methods#python dunder methods tutorial#python dunder#python special methods#magic methods in python#magic methods python#python tutorial#dunder methods python list#dunder magic methods python#magic methods#python class methods#dunder methods in python list#dunder methods in python in telugu#magic dunder method in python#python classes#python in telugu#dunder methods in python#python#dunder methods in python in telugu#python dunder methods#learn python in telugu#oops in telugu#dunder method in python#object orientated programming in telugu#object oriented programming in python#python tutorial in telugu#python language in telugu#oops in python#magic methods in python#python oops in telugu#python for beginners in telugu#python telugu#__del__ in python#__init__ in python
--------------------------------------------------------------------------------------------------------

operator overloading --   • #6 Operator Overloading in python||polymor...  

--------------------------------------------------------------------------------------

Dunder methods, short for "double underscore" methods, are special methods in Python that have double underscores at the beginning and end of their names (e.g., `__init__`, `__str__`). They are also known as "magic methods" because they enable the customization of class behavior and interact with Python's built-in functions and operators.

Key Points About Dunder Methods

1. **Special Naming**: Dunder methods are recognizable by their double underscores at both ends of their names.
2. **Built-in Interaction**: They allow objects to interact with Python's built-in functions and operators.
3. **Customization**: They enable the customization of how objects are created, represented, and manipulated.

Common Dunder Methods and Examples

1. **`__init__`**: Constructor method, called when an instance of a class is created.
```python
class Person:
def __init__(self, name, age):
self.name = name
self.age = age

person = Person("Alice", 30)
print(person.name) # Outputs: Alice
```

2. **`__str__`**: Called by the `str()` function and the `print` statement to provide a human-readable string representation of an object.
```python
class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def __str__(self):
return f"Person(name={self.name}, age={self.age})"

person = Person("Alice", 30)
print(person) # Outputs: Person(name=Alice, age=30)
```

3. **`__repr__`**: Called by the `repr()` function to provide an unambiguous string representation of an object, often used for debugging.
```python
class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def __repr__(self):
return f"Person(name={self.name}, age={self.age})"

person = Person("Alice", 30)
print(repr(person)) # Outputs: Person(name=Alice, age=30)
```

4. **`__len__`**: Called by the `len()` function to return the length of an object.
```python
class MyList:
def __init__(self, items):
self.items = items

def __len__(self):
return len(self.items)

my_list = MyList([1, 2, 3, 4])
print(len(my_list)) # Outputs: 4
```

5. **`__getitem__`, `__setitem__`, `__delitem__`**: Enable indexing, assignment, and deletion of items using square bracket notation.
```python
class MyList:
def __init__(self, items):
self.items = items

def __getitem__(self, index):
return self.items[index]

def __setitem__(self, index, value):
self.items[index] = value

def __delitem__(self, index):
del self.items[index]

my_list = MyList([1, 2, 3])
print(my_list[1]) # Outputs: 2
my_list[1] = 10
print(my_list[1]) # Outputs: 10
del my_list[1]
print(my_list.items) # Outputs: [1, 10]
```

6. **`__add__`**: Called by the `+` operator to add two objects.
```python
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)

def __repr__(self):
return f"Vector({self.x}, {self.y})"

v1 = Vector(2, 3)
v2 = Vector(3, 4)
v3 = v1 + v2
print(v3) # Outputs: Vector(5, 7)
```

Summary

Dunder methods are special methods in Python that allow developers to define how objects of a class behave with built-in functions and operators. By implementing these methods, you can customize object creation, representation, arithmetic operations, and more, making your classes more integrated with Python's features and easier to use.


En esta página del sitio puede ver el video en línea #10 Dunder Methods|Magic Methods|Special methods in python|python for beginners|OOP de Duración hora minuto segunda en buena calidad , que subió el usuario pythonbuzz 22 mayo 2024, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 187 veces y le gustó like a los espectadores. Disfruta viendo!