u9-script-classes-advanced Flashcards

1
Q

Question;Answer

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Given `f1 = Fraction(1

A

2) and f2 = Fraction(2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the correct implementation of \_\_eq\_\_ for the Fraction class?;```python\ndef __eq__(self

A

other):\n if isinstance(other

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does return NotImplemented do in special methods?;It indicates that the operation is not implemented for the given types

A

allowing Python to try other methods or raise a TypeError. This is different from raising NotImplementedError.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the purpose of the \_\_str\_\_ method in Python classes?;The \_\_str\_\_ method provides a human-friendly string representation of an object

A

used when calling str() on the object or when printing it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

A

self.d * other.d)`.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What’s the difference between \_\_add\_\_ and \_\_iadd\_\_ in Python?;\_\_add\_\_ creates a new object for regular addition (a + b)

A

while \_\_iadd\_\_ modifies the existing object for in-place addition (a += b).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How does the Fraction class handle multiplication with integers?;If the other operand is an integer

A

it multiplies only the numerator: `Fraction(self.n * other

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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

A

allowing the method to return NotImplemented for incompatible types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How does the Fraction class convert to float?;The \_\_float\_\_ method divides the numerator by the denominator: return self.n / self.d.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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).

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What types of indices are supported by the Indexer class?;Only integer indices are supported. Any other type will raise a TypeError.

17
Q

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.

18
Q

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.

19
Q

What’s the relationship between IndexDict and its Indexer class?;Each IndexDict instance creates an Indexer instance stored in its ‘index’ attribute

A

which provides numeric indexing capabilities to access dictionary items.

20
Q

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

A

making the class more robust and predictable.

21
Q

How does the Fraction class handle float operands in arithmetic operations?;It raises a TypeError by returning NotImplemented when a float operand is provided

A

as shown in the test cases where f1 + 2.0 raises TypeError.