-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
Special methods in Python, often referred to as "magic methods" or "dunder (double underscore) methods," are predefined methods with double underscores at the beginning and end of their names (e.g., `__init__`, `__str__`). These methods allow us to define how objects of a class behave in certain contexts, such as how they are created, represented as strings, or interact with operators. They enable Python's "data model," which is how objects work behind the scenes in Python. | ||
|
||
Here's an explanation of some common special methods, with examples to illustrate their use: | ||
|
||
### 1. `__init__`: Initialization (Constructor) | ||
- The `__init__` method is called when an object is created. It allows you to initialize attributes. | ||
|
||
```python | ||
class Person: | ||
def __init__(self, name, age): | ||
self.name = name | ||
self.age = age | ||
|
||
person = Person("Alice", 30) | ||
print(person.name) # Output: Alice | ||
``` | ||
|
||
### 2. `__str__`: String Representation | ||
- The `__str__` method defines how an object is represented as a string, commonly used when printing an object. | ||
|
||
```python | ||
class Person: | ||
def __init__(self, name, age): | ||
self.name = name | ||
self.age = age | ||
|
||
def __str__(self): | ||
return f"{self.name}, {self.age} years old" | ||
|
||
person = Person("Alice", 30) | ||
print(person) # Output: Alice, 30 years old | ||
``` | ||
|
||
### 3. `__repr__`: Official String Representation | ||
- The `__repr__` method provides a more detailed representation of an object, useful for debugging. It's meant to return a string that, ideally, could recreate the object. | ||
|
||
```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)) # Output: Person(name='Alice', age=30) | ||
``` | ||
|
||
### 4. `__len__`: Length of Object | ||
- The `__len__` method defines the behavior for the `len()` function, allowing you to specify what `len()` should return for instances of your class. | ||
|
||
```python | ||
class Book: | ||
def __init__(self, title, pages): | ||
self.title = title | ||
self.pages = pages | ||
|
||
def __len__(self): | ||
return self.pages | ||
|
||
book = Book("Python Programming", 350) | ||
print(len(book)) # Output: 350 | ||
``` | ||
|
||
### 5. `__add__`: Addition Operator (`+`) | ||
- The `__add__` method defines the behavior of the addition operator (`+`) for instances of your class. | ||
|
||
```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 __str__(self): | ||
return f"Vector({self.x}, {self.y})" | ||
|
||
v1 = Vector(2, 3) | ||
v2 = Vector(4, 5) | ||
print(v1 + v2) # Output: Vector(6, 8) | ||
``` | ||
|
||
### 6. `__getitem__`: Accessing Items (`[]`) | ||
- The `__getitem__` method allows an object to use the bracket notation (`[]`) for indexing. | ||
|
||
```python | ||
class MyList: | ||
def __init__(self, data): | ||
self.data = data | ||
|
||
def __getitem__(self, index): | ||
return self.data[index] | ||
|
||
my_list = MyList([10, 20, 30]) | ||
print(my_list[1]) # Output: 20 | ||
``` | ||
|
||
### 7. `__call__`: Callable Objects | ||
- The `__call__` method allows an instance to be called as if it were a function. | ||
|
||
```python | ||
class Greeting: | ||
def __init__(self, name): | ||
self.name = name | ||
|
||
def __call__(self, greet): | ||
return f"{greet}, {self.name}!" | ||
|
||
greet = Greeting("Alice") | ||
print(greet("Hello")) # Output: Hello, Alice! | ||
``` | ||
|
||
### 8. `__eq__`: Equality Comparison (`==`) | ||
- The `__eq__` method lets you define custom behavior for the equality operator (`==`). | ||
|
||
```python | ||
class Person: | ||
def __init__(self, name, age): | ||
self.name = name | ||
self.age = age | ||
|
||
def __eq__(self, other): | ||
return self.name == other.name and self.age == other.age | ||
|
||
p1 = Person("Alice", 30) | ||
p2 = Person("Alice", 30) | ||
print(p1 == p2) # Output: True | ||
``` | ||
|
||
These methods allow your classes to integrate seamlessly into Python’s syntax and behavior, making objects more intuitive to use and enhancing the readability and functionality of your code. |