create a dog in python in python

To create a dog in Python, we can use the OOP concept and create a class Dog. Below is an example implementation:

main.py
class Dog:
    def __init__(self, name, breed):
        self.name = name
        self.breed = breed
        self.tricks = []

    def teach_trick(self, trick):
        self.tricks.append(trick)

    def bark(self):
        print("Woof!")

my_dog = Dog("Buddy", "Labrador")
my_dog.bark() # prints "Woof!"
my_dog.teach_trick("sit")
print(my_dog.tricks) # prints ["sit"]
368 chars
17 lines

In the example above, we create a Dog class which has name, breed, and tricks attributes. The __init__ method is used to initialize these attributes when a new Dog instance is created. The teach_trick method is used to add tricks to the tricks list. Finally, we create a my_dog instance of the Dog class and call its bark and teach_trick methods.

gistlibby LogSnag