Exercise 1

You have a Dog class as shown below. The constructor sets up an instance of the class and specifies a list of dog colors and any special abilities. For example, a black lab color list would simply read ['black'] and the special abilities might be the string 'retrieving'.

In [1]:
class Dog:
    def __init__(self, breed, colors, special=None):
        self.breed = breed
        self.colors = colors
        self.special = special

Suppose you're reading in some data about dog breeds and for some reason you want to create instances of each breed. You might do something like:

In [2]:
my_dog = Dog('lab', ['black'], 'retrieving')
vars(my_dog)
Out[2]:
{'breed': 'lab', 'colors': ['black'], 'special': 'retrieving'}

Of course, every time you need a lab instance, you would need to remember these arguments. Instead, you could use classmethods as a factory to generate objects that you want. This may be useful as a pre-processing step in some workflow.

Exercise Task

Include a classmethod in the Dog class for a beagle (or any other dog of your choosing). Here's how it should work:

my_dog = Dog.beagle()
print((my_dog)) # returns <__main__.Dog object at 0x10a4bc320>
print(vars(my_dog)) # returns {'breed': 'beagle', 'colors': ['black', 'white', 'brown'], 'special': 'hunting'}

Deliverables

The exercise solution should be included in a file called L8E1.py. The Python file should include the class definition as well as the demo.