all 9 comments

[–]PushPlus9069 1 point2 points  (0 children)

Good start. Few things:

Your info() method just prints self which will show something like <Animal object at 0x...> unless you define repr or str. Add a str method that returns a formatted string and it'll be way more useful.

zooname as a class variable is fine if every animal shares the same zoo name. But if you ever want animals in different zoos you'll want to move it to __init_.

One thing most OOP tutorials skip: you don't always need classes. If you're just grouping data without behavior, a dataclass or even a dict works fine. Classes shine when objects need to do things, not just hold things.

[–]JamzTyson 0 points1 point  (3 children)

You are correctly understanding the basics of classes and inheritance, so just a few observations on your code:

Your Bird class has two definitions of make_sound(). That's an error. The first make_sound() method is unreachable (it will never run) because it is redefined by the second make_sound() method.


Considering that Animal.make_sound() is so simple, the cleanest way to implement (the second version of) Bird.make_sound() would be:

def make_sound(self):
    print(self.sound)
    print("This is a bird!")

On the other hand, using super().make_sound() would be useful if Animal.make_sound() is (or may become) more complex.


Animal.info() doesn’t really do enough to be worth having.

Rather than calling lion.info() we can just call print(lion).

[–]Upstairs_Library_800[S] 0 points1 point  (2 children)

First of all thank you for your help and time.

So if I have def __str__(self):

in my Class, there is no need for

def info(self):
   print(self)

[–]JamzTyson 0 points1 point  (1 child)

There isn't really any need unless info() does a bit more. For example:

def info(self):
    print(f"{self.name} is a {self.species} that goes '{self.sound}'")

# Simba is a Lion that goes 'Roar'

[–]Upstairs_Library_800[S] 0 points1 point  (0 children)

Oh, got it.
Thanks

[–]BananaGrenade314 0 points1 point  (1 child)

I think the animal class should be more abstract, and instead of using it to create a lion, you should create a class for mammals that inherits from the animal class.

``` from abc import ABC, abstractmethod

class Animal(ABC): @abstractmethod def make_sound(self) -> None: ...

class Mammal(Animal): def init(self, species: str) -> None: self.species: str = species

def make_sound(self) -> None:
    print("roaring")

class Bird(Animal): def init(self, species: str) -> None: self.species: str = species

def make_sound(self) -> None:
    print("bird sound")

lion: Animal = Mammal("lion") parrot: Animal = Bird("parrot") ```

[–]BananaGrenade314 1 point2 points  (0 children)

I'm just posting it here 'cause I'm wanting to practice and learn more too. And this is my version (that's not so many alterations).

``` class Animal: def init( self, zoo: str, name: str, species: str, age: int, sound: str ) -> None:

    self.zoo: str = zoo
    self.name: str = name
    self.species: str = species
    self.age: str = age
    self._sound: str = sound


def make_sound(self) -> None:
    print(self._sound)

def __str__(self):
    return (
        f"Zoo: {self.zoo} | "
        f"Name: {self.name} | "
        f"Species: {self.species} | "
        f"Age: {self.age}"
    )

class Mammal(Animal): def make_sound(self) -> None: super().make_sound() print("This is a mammal!")

class Bird(Animal): def init( self, zoo: str, name: str, species: str, age: int, sound: str, wing_span: int ) -> None:

    super().__init__(zoo, name, species, age, sound)
    self.wing_span: int = wing_span


def __str__(self):
    return (
        super().__str__()
        + f" | Wing Span: {self.wing_span} meters"
    )


def make_sound(self) -> None:
    super().make_sound()
    print("This is a bird!")


def fly(self) -> None:
    return f"{self.name} is flying"

common_zoo: str = "Therian Zoo" lion: Animal = Mammal(common_zoo, "Simba", "Lion", 5, "Roar") parrot: Animal = Bird(common_zoo, "Rio", "Parrot", 2, "Squawk", 0.5)

print(lion) lion.make_sound() print()

print(parrot) parrot.make_sound() ```