-
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 #928
base: master
Are you sure you want to change the base?
Solution #928
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.
in genaral looks good
app/main.py
Outdated
def __repr__(self) -> str: | ||
return f"Distance(km={self.km})" | ||
|
||
def __add__(self, other: Distance) -> 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.
you can add | float
to type annotations
app/main.py
Outdated
return f"Distance(km={self.km})" | ||
|
||
def __add__(self, other: Distance) -> Distance: | ||
if isinstance(other, 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.
this code duplicates several times
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.
agree, try to remove such duplication
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.
Yp, I created new static method for checking the type other. To make my code shorted
app/main.py
Outdated
return f"Distance(km={self.km})" | ||
|
||
def __add__(self, other: Distance) -> Distance: | ||
if isinstance(other, 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.
agree, try to remove such duplication
app/main.py
Outdated
class Distance: | ||
# Write your code here | ||
pass | ||
def __init__(self, km: float) -> 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.
I think km
could be int
as well
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.
I think user can input Float or Int types
app/main.py
Outdated
def __repr__(self) -> str: | ||
return f"Distance(km={self.km})" | ||
|
||
def __add__(self, other: Distance) -> 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.
Is other
only Distance instance?)
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.
add any types
app/main.py
Outdated
def __le__(self, other: Distance) -> bool: | ||
if isinstance(other, Distance): | ||
other = other.km | ||
return self.km <= other | ||
|
||
def __ge__(self, other: Distance) -> bool: | ||
if isinstance(other, Distance): | ||
other = other.km | ||
return 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 these methods with not
+ previously implemented operators.
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.
It's ingeniously simple
app/main.py
Outdated
def __truediv__(self, other: float | int) -> Distance: | ||
return Distance(km=round(self.km / other, 2)) | ||
|
||
def __lt__(self, other: Distance | float) -> bool: | ||
return self.km < self.convert_obj_number(obj=other) | ||
|
||
def __gt__(self, other: Distance | float) -> bool: | ||
return self.km > self.convert_obj_number(obj=other) | ||
|
||
def __eq__(self, other: Distance | float) -> bool: | ||
return self.km == self.convert_obj_number(obj=other) | ||
|
||
def __le__(self, other: Distance | float) -> bool: | ||
return not self.__gt__(other=other) | ||
|
||
def __ge__(self, other: Distance | float) -> bool: | ||
return not self.__lt__(other=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.
suggestion:
Distance | float | int
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.
Sure. Done!
app/main.py
Outdated
def __iadd__(self, other: Distance | float) -> Distance: | ||
self.km += self.convert_obj_number(obj=other) | ||
return self | ||
|
||
def __mul__(self, other: float) -> 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.
you can't add int here?)
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.
I can)
app/main.py
Outdated
def convert_obj_number(obj: Distance | float | int) -> float | int: | ||
if isinstance(obj, Distance): | ||
obj = obj.km | ||
return obj |
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.
That's pretty strange naming, I mean even if func name is convert_obj_to_number, why then you return obj, not simple obj.km?)
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.
I too had doubts about this function name)
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!
def get_km(obj: Distance | float | int) -> float | int: | ||
return obj.km if isinstance(obj, Distance) else obj |
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.
great!
No description provided.