top of page

Python Flexing: Abstractionists in Python, Painting the OOP Way

Zaktualizowano: 5 sie 2023

In this captivating journey through Python Flexing (a delightful brain coding yoga, a stretch for the mind!), we venture into the captivating realm of Abstractionists in Python, where programming takes on the form of art.


Embrace the creative spirit of abstractionist painters as we delve into the world of Object-Oriented Programming (OOP) and unleash the beauty of abstraction in Python.

With coding exercises inspired by the brushstrokes of legendary artists, you'll master the art of abstracting classes and methods, elevating your Python prowess to craft elegant and expressive code. Join me in painting the OOP way, where Python becomes a canvas of endless possibilities, and your imagination takes center stage.


Let's Python Flex and express our coding brilliance like true Abstractionists!


To the point. In Python, the architecture has been designed to be flat by principle.


In more complex languages like Java, CPP, or C#, abstract classes are foundational coding pillars, providing programmers with certain constraints. However, in Python, abstraction, in general, is not emphasized to allow greater flexibility.


Nonetheless, you can indeed use abstract classes and methods in Python by inheriting from the ABC metaclass and annotating methods as abstract. The ABC class is particularly valuable when creating a Singleton in Python.

from abc import ABC, abstractmethod

class Artist(ABC):
    @property
    @abstractmethod
    def Artwork(self):
        pass
    
    @property
    @abstractmethod
    def Medium(self):
        pass
    
    def create_noise(self):
        print("Creating abstract art...")
    
    def __name__(self):
        print("This is an abstract class Artist")

add in the same file class Pollock that inherits from Artist


class Pollock(Artist):
    @property
    def Artwork(self):
        return "Number 1A, 1948"
    
    def create_noise(self):
        print("Drip, splash, splatter!")

same with Picasso that inherits from Artist

class Picasso(Artist):
    @property
    def Artwork(self):
        return "Les Demoiselles d'Avignon"

    @property
    def Medium(self):
        return "Oil on Canvas"
    
    def create_noise(self):
        print("Cubism at its finest!")


and class Dali (also inherits from Artist)

class Dali(Artist):
    @property
    def Artwork(self):
        return "The Persistence of Memory"

    @property
    def Medium(self):
        return "Oil on Canvas"
    
    def create_noise(self):
        print("Surrealistic dreamscape!")


Testing the classes

artist = Artist() # returns TypeError: Can't instantiate abstract class Artist with abstract methods Artwork, Medium


pollock = Pollock() # TypeError: Can't instantiate abstract class Pollock with abstract method Medium


# Challenge 1: Fix the Artist class

# Challenge 2: Fix the Pollock class

# Challenge 3: Add new class by yourself.

HINTS:

# Artist is a derived class from ABC and implements the Artwork property and overrides the create_noise method.

# Pollock is a derived class from Artist and overrides the Artwork property and create_noise method.

# Picasso is another derived class from Artist and adds the Medium property, overrides the Artwork property, and the create_noise method.

# Dali is another derived class from Artist and adds the Medium property, overrides the Artwork property, and the create_noise method.

# Below is UML diagram to hel you visualize things.


Challenges for you:

Challenge 1: Fix the Artist class: The Artist class is currently missing the implementation for the abstract properties Artwork and Medium. To fix it, you need to provide concrete implementations for these properties in the Artist class.


Challenge 2: Fix the Pollock class: Similar to the Artist class, the Pollock class is missing the implementation for the abstract property Medium. You need to provide a concrete implementation for the Medium property in the Pollock class.


Challenge 3: Add a new class: You can add a new class of your own, deriving from Artist or any other appropriate class. Define new properties and methods specific to your class, and let your creativity flow like an abstractionist painter!


GOOD LUCK! Feel free to share your ideas for challenge 3!

UML diagram

(I can`t stretch it so you have to imagine that all arrows point to Artist):


+----------------------------------+
|             Artist               |
+----------------------------------+
|    - Artwork: property (abstract)|
|    - Medium: property (abstract) |
+----------------------------------+
|    + create_noise(): void        |
|    + __name__(): void            |
+----------------------------------+
         ^     ^     ^   
         |     |     |
         |     |     |
+----------------------------------+
|             Pollock              |
+----------------------------------+
|    - Artwork: property           |
+----------------------------------+
|    + create_noise(): void        |
+----------------------------------+
               |     |
               |     |
               |     |
+----------------------------------+
|             Picasso              |
+----------------------------------+
|    - Artwork: property           |
|    - Medium: property            |
+----------------------------------+
|    + create_noise(): void        |
+----------------------------------+
                     |
                     |
                     |
+----------------------------------+
|               Dali               |
+----------------------------------+
|    - Artwork: property           |
|    - Medium: property            |
+----------------------------------+
|    + create_noise(): void        |
+----------------------------------+

The UML (Unified Modeling Language) diagram is a visual representation of the structure and relationships between classes and objects in an object-oriented system. It serves as a powerful tool to visualize and communicate the architecture and design of a software application. The UML diagram is helpful for several reasons:

  1. Visualization of Class Hierarchy: The UML diagram provides a clear and concise representation of the inheritance hierarchy, showing how classes are related through inheritance and interfaces. This helps developers understand the class relationships and the overall structure of the system.

  2. Abstraction of Complex Concepts: UML diagrams abstract complex concepts, making them easier to understand for both technical and non-technical stakeholders. It simplifies the communication between developers, designers, project managers, and other team members, facilitating better collaboration and reducing misunderstandings.

  3. Modeling Behavior and Interactions: UML diagrams can also depict the behavior and interactions between objects using sequence diagrams, activity diagrams, state diagrams, and more. This allows developers to visualize how different components interact and respond to various events, aiding in the analysis and design of the application's functionality.

  4. Documentation and Maintenance: UML diagrams act as valuable documentation of the software architecture. They provide a clear and standardized way to document the design decisions and thought process behind the system, making it easier for future developers to understand and maintain the codebase.

  5. Design Validation and Refinement: UML diagrams can be used to validate the design decisions before implementing them. By visualizing the system's structure and interactions early in the development process, developers can identify potential design flaws and make improvements, leading to a more robust and efficient system.

  6. Code Generation: In some cases, UML diagrams can be used to generate code automatically or semi-automatically, reducing the manual effort required for implementation and ensuring consistency between the design and code.



Comments


Subscribe to QABites newsletter

Thanks for submitting!

  • Twitter
  • Facebook
  • Linkedin

© 2023 by QaBites. Powered and secured by Wix

bottom of page