|
124 | 124 | And it does so.
|
125 | 125 | This is because the two objects~\pythonil{p1} and~\pythonil{5} are not identical.
|
126 | 126 | The default equality comparison only checks for identity.
|
127 |
| -If implement \pyhonilIdx{\_\_eq\_\_} by ourselves, this method should clearly return a value that makes \pythonil{p1 == 5} become~\pythonil{False} as well. |
| 127 | +If implement \pythonilIdx{\_\_eq\_\_} by ourselves, this method should clearly return a value that makes \pythonil{p1 == 5} become~\pythonil{False} as well. |
128 | 128 | Anything else would be nonsense.
|
129 | 129 |
|
130 | 130 | \gitPython{\programmingWithPythonCodeRepo}{09_dunder/point_with_dunder.py}{--args format}{dunder:point_with_dunder}{%
|
131 |
| -Our \pythonil{Point} class, extended with the \pythonilIdx{\_\_str\_\_}\pythonIdx{dunder!\_\_str\_\_}, \pythonilIdx{\_\_repr\_\_}\pythonIdx{dunder!\_\_repr\_\_}, and \pythonilIdx{\_\_eq\_\_}\pythonIdx{dunder!\_\_eq\_\_} dunder methods.}% |
| 131 | +Our \pythonil{Point} class, extended with the \pythonilIdx{\_\_str\_\_}\pythonIdx{dunder!\_\_str\_\_}, \pythonilIdx{\_\_repr\_\_}\pythonIdx{dunder!\_\_repr\_\_}, and \pythonilIdx{\_\_eq\_\_}\pythonIdx{dunder!\_\_eq\_\_}\pythonIdx{NotImplementedType} dunder methods.}% |
132 | 132 | %
|
133 | 133 | \gitPythonAndOutput{\programmingWithPythonCodeRepo}{09_dunder}{point_with_dunder_user.py}{--args format}{dunder:point_with_dunder_user}{%
|
134 | 134 | The same program exploring string representations and equality as shown in \cref{lst:dunder:point_user_2}, but this time using our new \pythonil{Point} class from \cref{lst:dunder:point_with_dunder}.}%
|
|
143 | 143 | Otherwise, it will return the constant \pythonilIdx{NotImplemented}:%
|
144 | 144 | %
|
145 | 145 | \quotation{PSF2024BIC}{%
|
146 |
| -A special value which should be returned by the binary special methods [\dots] to indicate that the operation is not implemented with respect to the other type\dots\medskip\\\indent% |
| 146 | +A special value which should be returned by the binary special methods [\dots] to indicate that the operation is not implemented with respect to the other type\dots\medskip\\\strut\hspace{1cm}\strut% |
147 | 147 | \emph{Note:}~When a binary (or in-place) method returns \pythonilIdx{NotImplemented} the interpreter will try the reflected operation on the other type (or some other fallback, depending on the operator). %
|
148 | 148 | If all attempts return \pythonilIdx{NotImplemented}, the interpreter will raise an appropriate exception. %
|
149 | 149 | Incorrectly returning \pythonilIdx{NotImplemented} will result in a misleading error message or the \pythonilIdx{NotImplemented} value being returned to \python\ code.
|
|
154 | 154 | Now, we could return \pythonil{False} in this case, which would be fine as well.
|
155 | 155 | Returning \pythonilIdx{NotImplemented} will give us the same result in comparisons with objects of other types~(like~\pythonil{5}).
|
156 | 156 | However, it keeps an avenue open for other programmers to design new classes which support comparison with our \pythonil{Point} instances in a consistent way.
|
| 157 | +When we implement the \pythonilIdx{\_\_eq\_\_} method like this, the proper \pgls{typeHint} for the return value is \pythonil{bool | NotImplementedType}\pythonIdx{NotImplementedType}. |
157 | 158 |
|
158 | 159 | \Cref{lst:dunder:point_with_dunder_user} is the same as \cref{lst:dunder:point_user_2}, but now uses this new variant of our class \pythonil{Point}.
|
159 | 160 | As you can see in \cref{exec:dunder:point_with_dunder_user}, its output now matches much better to what one would expect.%
|
|
0 commit comments