Python Design Patterns Object-Oriented Patterns
Python Design Patterns: Object-Oriented Design
The object-oriented design pattern is the most commonly used design pattern. This pattern can be found in almost every programming language.
How to Implement the Object-Oriented Design Pattern
Now let’s look at how to implement the object-oriented design pattern.
class Parrot:
# class attribute
species = "bird"
# instance attribute
def __init__(self, name, age):
self.name = name
self.age = age
# instantiate the Parrot class
blu = Parrot("Blu", 10)
woo = Parrot("Woo", 15)
# access the class attributes
print("Blu is a {}".format(blu.__class__.species))
print("Woo is also a {}".format(woo.__class__.species))
# access the instance attributes
print("{} is {} years old".format( blu.name, blu.age))
print("{} is {} years old".format( woo.name, woo.age))
Output
The above program produces the following output
Explanation
This code includes both class attributes and instance attributes, which are printed according to the output requirements. Various features form part of the object-oriented pattern. These features will be explained in the next chapter.