Skip to content
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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

motivated2
Copy link

No description provided.

app/main.py Outdated
def __repr__(self) -> str:
return f"Distance(km={self.km})"

def __add__(self, other: Distance | int) -> Distance:

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

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

Comment on lines +36 to +40
@staticmethod
def to_number(number: Distance | int | float) -> int | float:
if isinstance(number, Distance):
return number.km
return number

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

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

Copy link

@Serhii-Leonenko Serhii-Leonenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GJ!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants