u9-script-classes-advanced Flashcards
Question;Answer
What is the purpose of the \_\_eq\_\_
method in Python classes?;The \_\_eq\_\_
method enables equality checks between objects. It defines how two objects should be compared for equality using the == operator.
Given `f1 = Fraction(1
2) and
f2 = Fraction(2
What is the correct implementation of \_\_eq\_\_
for the Fraction class?;```python\ndef __eq__(self
other):\n if isinstance(other
What does return NotImplemented
do in special methods?;It indicates that the operation is not implemented for the given types
allowing Python to try other methods or raise a TypeError. This is different from raising NotImplementedError.
What is the purpose of the \_\_str\_\_
method in Python classes?;The \_\_str\_\_
method provides a human-friendly string representation of an object
used when calling str() on the object or when printing it.
How does the Fraction class implement fraction addition with another fraction?;It multiplies the numerators and denominators cross-wise: (a/b + c/d = (a*d + c*b)/(b*d))
. This is implemented in \_\_add\_\_
by returning `Fraction(self.n * other.d + other.n * self.d
self.d * other.d)`.
What’s the difference between \_\_add\_\_
and \_\_iadd\_\_
in Python?;\_\_add\_\_
creates a new object for regular addition (a + b)
while \_\_iadd\_\_
modifies the existing object for in-place addition (a += b).
How does the Fraction class handle multiplication with integers?;If the other operand is an integer
it multiplies only the numerator: `Fraction(self.n * other
What is the purpose of isinstance()
checks in special methods?;They ensure type safety by checking if the operation is being performed with compatible types
allowing the method to return NotImplemented for incompatible types.
How does the Fraction class convert to float?;The \_\_float\_\_
method divides the numerator by the denominator: return self.n / self.d
.
What is the purpose of the IndexDict class?;It extends the regular dictionary to support both dictionary-style access and sequence-style numeric indexing through a special ‘index’ attribute.
How does the IndexDict class handle negative indices?;It converts negative indices to positive by adding the length of the dictionary: key = key + len(self._index_dict)
.
What happens when you try to access an invalid index in IndexDict?;It raises an IndexError with the message “Index out of range” if the index is out of bounds after converting negative indices.
What is the purpose of the inner Indexer class in IndexDict?;It provides sequence-like access to dictionary items through numeric indices while keeping the main dictionary functionality intact.
What types of indices are supported by the Indexer class?;Only integer indices are supported. Any other type will raise a TypeError.
How does \_\_getitem\_\_
in the Indexer class find the correct item?;It uses enumerate to iterate through the dictionary items until it finds the item at the requested index position.
What happens if you try to use a string index with IndexDict’s index attribute?;It raises a TypeError because the Indexer class only supports integer indices.
What’s the relationship between IndexDict and its Indexer class?;Each IndexDict instance creates an Indexer instance stored in its ‘index’ attribute
which provides numeric indexing capabilities to access dictionary items.
What is the purpose of type checking in special methods?;It ensures operations are only performed with compatible types and provides appropriate error handling for incompatible types
making the class more robust and predictable.
How does the Fraction class handle float operands in arithmetic operations?;It raises a TypeError by returning NotImplemented when a float operand is provided
as shown in the test cases where f1 + 2.0
raises TypeError.