Escaped underscores in dunder methods
This commit is contained in:
@@ -12,7 +12,7 @@ item = x[3]
|
|||||||
print(item) # Output: Item at index 3
|
print(item) # Output: Item at index 3
|
||||||
```
|
```
|
||||||
```Python
|
```Python
|
||||||
# Equivalent to type(x).__getitem__(x, 3)
|
# Equivalent to type(x).\_\_getitem\_\_(x, 3)
|
||||||
item = type(x).__getitem__(x, 3)
|
item = type(x).__getitem__(x, 3)
|
||||||
print(item) # Output: Item at index 3
|
print(item) # Output: Item at index 3
|
||||||
```
|
```
|
||||||
@@ -20,35 +20,36 @@ print(item) # Output: Item at index 3
|
|||||||
---
|
---
|
||||||
## 3.3.1
|
## 3.3.1
|
||||||
|
|
||||||
1. The object.__new__() method is a special method in Python that is called to create a new instance of a class. It is responsible for the low-level creation of an object before its initialization.
|
|
||||||
|
|
||||||
2. The object.__init__() method is a special method in Python that is called after an instance of a class has been created by the __new__() method. It is responsible for initializing the object's attributes and performing any necessary setup.
|
1. The object.\_\_new\_\_() method is a special method in Python that is called to create a new instance of a class. It is responsible for the low-level creation of an object before its initialization.
|
||||||
|
|
||||||
3. The object.__del__() method is a special method in Python that is called when an instance of a class is about to be destroyed or garbage-collected.
|
2. The object.\_\_init\_\_() method is a special method in Python that is called after an instance of a class has been created by the \_\_new\_\_() method. It is responsible for initializing the object's attributes and performing any necessary setup.
|
||||||
|
|
||||||
|
3. The object.\_\_del\_\_() method is a special method in Python that is called when an instance of a class is about to be destroyed or garbage-collected.
|
||||||
|
|
||||||
4. The cyclic garbage collector in CPython is responsible for detecting and breaking reference cycles, ensuring that memory is properly deallocated even in the presence of reference cycles.
|
4. The cyclic garbage collector in CPython is responsible for detecting and breaking reference cycles, ensuring that memory is properly deallocated even in the presence of reference cycles.
|
||||||
|
|
||||||
5. the __format__() method allows objects to define their own customized string representations when used with format-related operations.
|
5. the \_\_format\_\_() method allows objects to define their own customized string representations when used with format-related operations.
|
||||||
|
|
||||||
6. x < y calls x.__lt__(y)
|
6. x < y calls x.\_\_lt\_\_(y)
|
||||||
x <= y calls x.__le__(y)
|
x <= y calls x.\_\_le\_\_(y)
|
||||||
x == y calls x.__eq__(y)
|
x == y calls x.\_\_eq\_\_(y)
|
||||||
x != y calls x.__ne__(y)
|
x != y calls x.\_\_ne\_\_(y)
|
||||||
x > y calls x.__gt__(y)
|
x > y calls x.\_\_gt\_\_(y)
|
||||||
x >= y calls x.__ge__(y)
|
x >= y calls x.\_\_ge\_\_(y)
|
||||||
|
|
||||||
---
|
---
|
||||||
## 3.3.2
|
## 3.3.2
|
||||||
|
|
||||||
### 3.3.2.1
|
### 3.3.2.1
|
||||||
|
|
||||||
1. The methods __get__, __set__, and __delete__ are used to implement descriptors in Python. Descriptors are a way to customize attribute access in classes. These methods are defined in a descriptor class, and when an instance of the descriptor class is present in an owner class, they are invoked to control attribute access.
|
1. The methods \_\_get\_\_, \_\_set\_\_, and \_\_delete\_\_ are used to implement descriptors in Python. Descriptors are a way to customize attribute access in classes. These methods are defined in a descriptor class, and when an instance of the descriptor class is present in an owner class, they are invoked to control attribute access.
|
||||||
|
|
||||||
### 3.3.2.2
|
### 3.3.2.2
|
||||||
1. In Python, a descriptor is an object attribute that has "binding behavior" due to the overriding of attribute access through methods in the descriptor protocol: __get__(), __set__(), and __delete__(). If an object defines any of these methods, it is considered a descriptor.
|
1. In Python, a descriptor is an object attribute that has "binding behavior" due to the overriding of attribute access through methods in the descriptor protocol: \_\_get\_\_(), \_\_set\_\_(), and \_\_delete\_\_(). If an object defines any of these methods, it is considered a descriptor.
|
||||||
|
|
||||||
### 3.3.2.4
|
### 3.3.2.4
|
||||||
1. Using __slots__ can save significant memory compared to using __dict__, and it can also improve attribute lookup speed.
|
1. Using \_\_slots\_\_ can save significant memory compared to using \_\_dict\_\_, and it can also improve attribute lookup speed.
|
||||||
|
|
||||||
---
|
---
|
||||||
## 3.3.3
|
## 3.3.3
|
||||||
@@ -67,7 +68,7 @@ This code sets up a base class called Philosopher that defines a custom behavior
|
|||||||
|
|
||||||
The default_name parameter allows each subclass to have a default name attribute, and the AustralianPhilosopher subclass demonstrates the usage by setting its default_name to "Bruce".
|
The default_name parameter allows each subclass to have a default name attribute, and the AustralianPhilosopher subclass demonstrates the usage by setting its default_name to "Bruce".
|
||||||
|
|
||||||
1. The purpose of __set_name__() is to provide a hook for objects to perform any necessary initialization or setup when they are assigned to a class attribute. It allows the objects to be aware of their assigned name within the class they belong to, which can be useful for various purposes such as introspection or customization.
|
1. The purpose of \_\_set_name\_\_() is to provide a hook for objects to perform any necessary initialization or setup when they are assigned to a class attribute. It allows the objects to be aware of their assigned name within the class they belong to, which can be useful for various purposes such as introspection or customization.
|
||||||
|
|
||||||
### 3.3.3.1
|
### 3.3.3.1
|
||||||
1. The C3 linearization algorithm is a method used to determine the order in which base classes are searched for a particular attribute or method during multiple inheritance in Python. It ensures that the resolution order respects the consistency and priority of the inheritance hierarchy.
|
1. The C3 linearization algorithm is a method used to determine the order in which base classes are searched for a particular attribute or method during multiple inheritance in Python. It ensures that the resolution order respects the consistency and priority of the inheritance hierarchy.
|
||||||
@@ -86,9 +87,9 @@ def add(a: int, b: int) -> int:
|
|||||||
```
|
```
|
||||||
|
|
||||||
### 3.3.5.1
|
### 3.3.5.1
|
||||||
1. The purpose of __class_getitem__() is to allow runtime parameterization of standard-library generic classes in order to more easily apply type hints to these classes.
|
1. The purpose of \_\_class_getitem\_\_() is to allow runtime parameterization of standard-library generic classes in order to more easily apply type hints to these classes.
|
||||||
|
|
||||||
The __class_getitem__() method is called when you use square brackets notation ([]) with a generic class to specify the type arguments. For example:
|
The \_\_class_getitem\_\_() method is called when you use square brackets notation ([]) with a generic class to specify the type arguments. For example:
|
||||||
|
|
||||||
```Python
|
```Python
|
||||||
from typing import List
|
from typing import List
|
||||||
@@ -103,9 +104,9 @@ my_instance = MyGenericClass[int]()
|
|||||||
|
|
||||||
In Python, generic classes are classes that can operate on different types of objects or collections in a flexible way. They are typically defined with type variables that represent the types of the objects or collections they work with. However, when using these generic classes, you need a way to specify the actual types you want to use.
|
In Python, generic classes are classes that can operate on different types of objects or collections in a flexible way. They are typically defined with type variables that represent the types of the objects or collections they work with. However, when using these generic classes, you need a way to specify the actual types you want to use.
|
||||||
|
|
||||||
In the example above, 'MyGenericClass[int]()' invokes the __class_getitem__() method of MyGenericClass to specify that the type argument for the generic class is int. This allows you to parameterize the generic class at runtime and apply type hints to it.
|
In the example above, 'MyGenericClass[int]()' invokes the \_\_class_getitem\_\_() method of MyGenericClass to specify that the type argument for the generic class is int. This allows you to parameterize the generic class at runtime and apply type hints to it.
|
||||||
|
|
||||||
By implementing __class_getitem__() in your custom classes, you can enable similar behavior and allow runtime parameterization of your own generic classes.
|
By implementing \_\_class_getitem\_\_() in your custom classes, you can enable similar behavior and allow runtime parameterization of your own generic classes.
|
||||||
|
|
||||||
2. What is the runtime parameterization?
|
2. What is the runtime parameterization?
|
||||||
|
|
||||||
@@ -138,15 +139,15 @@ class FileManager:
|
|||||||
with FileManager('example.txt') as file:
|
with FileManager('example.txt') as file:
|
||||||
file.write('Hello, world!')
|
file.write('Hello, world!')
|
||||||
```
|
```
|
||||||
1. The *'__enter__'* method is responsible for setting up the runtime context, which in this case involves opening the specified file in write mode. The method returns the file object, which is then bound to the variable *'file'* in the *'with'* statement.
|
1. The *'\_\_enter\_\_'* method is responsible for setting up the runtime context, which in this case involves opening the specified file in write mode. The method returns the file object, which is then bound to the variable *'file'* in the *'with'* statement.
|
||||||
|
|
||||||
2. The *'__exit__'* method is called when exiting the runtime context. It is responsible for cleaning up any resources, in this case, closing the file. If an exception occurred within the *'with'* block, the exception information is passed to the *'__exit__'* method, allowing you to handle any necessary cleanup or error handling.
|
2. The *'\_\_exit\_\_'* method is called when exiting the runtime context. It is responsible for cleaning up any resources, in this case, closing the file. If an exception occurred within the *'with'* block, the exception information is passed to the *'\_\_exit\_\_'* method, allowing you to handle any necessary cleanup or error handling.
|
||||||
|
|
||||||
3. By using the *'with'* statement, the context manager automatically takes care of opening and closing the file for us. This ensures that the file is always closed, even if an exception occurs within the *'with'* block.
|
3. By using the *'with'* statement, the context manager automatically takes care of opening and closing the file for us. This ensures that the file is always closed, even if an exception occurs within the *'with'* block.
|
||||||
|
|
||||||
4. An explanation of the parameters in the *'__exit__'* method:
|
4. An explanation of the parameters in the *'\_\_exit\_\_'* method:
|
||||||
|
|
||||||
**self**: The self parameter refers to the instance of the class itself. It allows you to access the attributes and methods of the class within the '__exit__' method.
|
**self**: The self parameter refers to the instance of the class itself. It allows you to access the attributes and methods of the class within the '\_\_exit\_\_' method.
|
||||||
|
|
||||||
**exc_type**: This parameter represents the type of exception that occurred, if any. It will be None if no exception occurred within the with block.
|
**exc_type**: This parameter represents the type of exception that occurred, if any. It will be None if no exception occurred within the with block.
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user