Tudor Surdoiu
Mar 3, 2023

Hi, nice article. Thank you.

Just wanted to add that your example is more suited for a class oriented programming language like Java. For python there are more straightforward ways to achieve the "visitor" effect.

One example would be to use singledispatch:

```python

from functools import singledispatch

@singledispatch

def visit(artwork: Artwork):

print(f"Visting {artwork.title}")

@visit.register

def visit_painting(artwork: Painting):

print(f"Protecting {artwork.title} by {artwork.artist}")

@visit.register

def visit_sculpture(artwork: Sculpture):

print(f"Cleaning {artwork.title} by {artwork.artist}")

@visit.register

def visit_instalation(artwork: Installation):

print(f"Maintaining {artwork.title} by {artwork.artist}")

for artwork in self.artworks:

visit(artwork)

```

Or if you don't want to use singledispatch, you can just have a simple dict with the type of each artwork subclass as key and a coresponding visit function as value.

Thank you again!

Tudor Surdoiu
Tudor Surdoiu

Written by Tudor Surdoiu

Bio digital jazz writer, sometimes knocking on the sky and listening to the sound.

Responses (1)