209k views
5 votes
Write a class definition line and a one line docstring for the class Dog. Write an __init__ method for the class Dog that gives each dog its own name and breed. Make sure that you test this on a successful creation of a dog object. For example,>>> import dog>>> sugar = dog.Dog('Sugar', 'border collie')>>> sugar.nameSugar>>> sugar.breedborder collie

1 Answer

0 votes

Answer:

class Dog:

"""A dog with name and breed."""

def __init__(self, name, breed):

self.name = name

self.breed = breed

import dog

if __name__ == '__main__':

sugar = dog.Dog('sugar', 'border collie')

print(sugar.name)

print(sugar.breed)

Step-by-step explanation:

CLASS: A class describes what an object will be, it contains methods of functions and it is defined by using the key word class.

class Dog:

#The class is defined

Docstring: It is a short form of documentation strings, it provides a convenient way of associating documentation with Python modules, functions, classes, and methods. i.e, it tells you what they do.

it is declared by using triple quotes.

"""A dog with name and breed."""

Next we define the method and and assign attributes (Name and Breed) to the class.

def __init__(self, name, breed):

self.name = name

self.breed = breed

we import the class next: to do this ensure that you save your source file with the name dog.

Finally, we print out its attributes.

import dog

if __name__ == '__main__':

sugar = dog.Dog('sugar', 'border collie')

print(sugar.name)

print(sugar.breed)

I have attached a picture for you to see how the code runs

Write a class definition line and a one line docstring for the class Dog. Write an-example-1
User Qwadrox
by
5.2k points