-
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
implemented class Distance and its methods in main.py #945
base: master
Are you sure you want to change the base?
Conversation
app/main.py
Outdated
def __repr__(self) -> str: | ||
return f"Distance(km={self.km})" | ||
|
||
def __add__(self, other: Distance | int) -> Distance: |
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.
other
could be float as well
app/main.py
Outdated
return Distance(round(self.km / other, 2)) | ||
|
||
def __eq__(self, other: Distance | int | float) -> bool: | ||
if isinstance(other, (int, float)): |
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.
DRY, you duplicate this check in other methods, try to move this logic to a separate method
@staticmethod | ||
def to_number(number: Distance | int | float) -> int | float: | ||
if isinstance(number, Distance): | ||
return number.km | ||
return number |
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.
nice
app/main.py
Outdated
return self | ||
self.km += other.km | ||
def __add__(self, other: Distance | int | float) -> Distance: | ||
return Distance(self.km + Distance.to_number(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 call this method from self self.to_number(...)
. The same in all other similar cases
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.
GJ!
No description provided.