-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Carnivores and Herbivores Solution #728
base: master
Are you sure you want to change the base?
Conversation
tests/test_main.py
Outdated
@@ -129,6 +129,8 @@ def test_print_animal_alive(): | |||
|
|||
f = io.StringIO() | |||
|
|||
print(Animal.alive) | |||
|
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.
dont change the tests))
app/main.py
Outdated
if herbivore.hidden or isinstance(herbivore, Carnivore): | ||
return | ||
herbivore.health = max(0, herbivore.health - 50) | ||
if not herbivore.health: | ||
Animal.alive.pop(Animal.alive.index(herbivore)) |
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.
can we resolve it a little bit simpler?
app/main.py
Outdated
|
||
|
||
class Carnivore(Animal): | ||
def bite(self, herbivore: Herbivore) -> 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.
you don't need self here, so make this method static
app/main.py
Outdated
name = f"Name: {self.name}, " | ||
health = f"Health: {self.health}, " | ||
hidden = f"Hidden: {self.hidden}" | ||
return "{" + name + health + hidden + "}" |
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.
rewrite with only return, use f-strings
app/main.py
Outdated
|
||
class Carnivore(Animal): | ||
@staticmethod | ||
def bite(herbivore: Herbivore) -> 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.
Fix annotations and variable name, if this method would take only Herbivore instances, you won't need a check in the next line
app/main.py
Outdated
if herbivore.hidden or isinstance(herbivore, Carnivore): | ||
return |
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 use the opposite condition
app/main.py
Outdated
return | ||
herbivore.health -= 50 | ||
if herbivore.health <= 0: | ||
Animal.alive.pop(Animal.alive.index(herbivore)) |
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.
u can use .remove(value)
app/main.py
Outdated
return f"{{Name: {self.name}, " \ | ||
f"Health: {self.health}, " \ | ||
f"Hidden: {self.hidden}}}" |
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 brackets instead of linebreaker here
app/main.py
Outdated
class Carnivore(Animal): | ||
@staticmethod | ||
def bite(herbivore: Animal) -> None: | ||
if not (herbivore.hidden or isinstance(herbivore, Carnivore)): |
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.
What if later there will be another class that inherits from Animal? You should fix this condition, so it will be True only with Herbivore instance
app/main.py
Outdated
|
||
class Carnivore(Animal): | ||
@staticmethod | ||
def bite(herbivore: Animal) -> 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.
Fix variable name, it is not descriptive now, you use it not only for herbivores
app/main.py
Outdated
|
||
class Carnivore(Animal): | ||
@staticmethod | ||
def bite(herbivore: Herbivore | Carnivore) -> 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 Animal for annotations here and fix parameter name, it is not descriptive now
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.
well done ^_^
No description provided.