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

solution #1434

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
@@ -1 +1,30 @@
# write your code here
class Animal:
alive = list()

Choose a reason for hiding this comment

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

The alive list is a class variable, meaning it is shared among all instances of Animal. This could lead to unexpected behavior if not managed carefully, especially in a multi-threaded environment or if instances are created and destroyed frequently. Ensure this aligns with your design intentions.

Choose a reason for hiding this comment

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

The alive list is a class variable shared among all instances of Animal. This could lead to unexpected behavior, especially in multi-threaded environments or when instances are frequently created and destroyed. Consider using an instance variable if this is not the intended design.

def __init__(
self, name: str, health: int = 100, hidden: bool = False) -> None:
self.name = name
self.health = health
self.hidden = hidden
self.alive.append(self)

def __repr__(self) -> str:
animal = (f"{{Name: {self.name}, Health: {self.health},"

Choose a reason for hiding this comment

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

The __repr__ method is expected to return a string representation of the object. The current implementation returns a string, which is correct, but if the intention was to return a dictionary-like structure, consider renaming the method or adjusting its return value to match the intended representation.

f" Hidden: {self.hidden}}}")
return animal


class Herbivore(Animal):
def hide(self) -> None:
if self.hidden:
self.hidden = False
else:
self.hidden = True


class Carnivore(Animal):
def bite(self, another_animal: Animal) -> None:
if not another_animal.hidden and isinstance(another_animal, Herbivore):
another_animal.health -= 50
if another_animal.health < 1:
Animal.alive.remove(another_animal)
Loading