Skip to content
Advertisement

Class Instantiation, and variable sharing for abstract/static classes

Simple example of my problem:

JavaScript

The issue is that all services in this instance will share subscribers, hence my solution was to create a function that returns the abstract service as such:

JavaScript

This now works because subscribers are unique to each class inheriting the abstract service.

Issues

  1. Is this correct for python, or is the pattern wrong if so what would be the norm in this situation

  2. How do I handle typing for AbstractService if my solution is correct (for any function that requires that type to be passed to it)

Advertisement

Answer

i would approach it like this:

JavaScript

it sets a new subscribers instance on each class after creation.

edit: additionally this handles typing for you as well by using from __future__ import annotations

e.g.

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement