-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
'Solution' #907
base: master
Are you sure you want to change the base?
'Solution' #907
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Think about changing default python behavior, which we don't want to change, like
3 + None
And refactor your code)
app/main.py
Outdated
elif isinstance(other, (int, float)): | ||
return Distance(self.km + other) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can simplify the add method by returning the result of the addition directly without separate checks for int, or float. Python will automatically raise a TypeError if the other object is not of a type that supports addition with the types in your method. This simplifies the code and leverages Python's built-in type checking for arithmetic operations.
While your code will do nothing (return None) in case, for example, where we will try to add string to Distance object, which is unexpected behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation!
app/main.py
Outdated
if isinstance(other, Distance): | ||
return None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need these lines?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before receiving your comment I misunderstood that Python returns None in such case. Many thanks!
app/main.py
Outdated
self.km += other | ||
return self | ||
|
||
def __mul__(self, other: (Distance, int, float)) -> (Distance, None): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use Union or |
Since for now this method should return a tuple, according to your annotation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apply the principles and logic that you refactored in the last pushes to the entire app.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job!
No description provided.