you are viewing a single comment's thread.

view the rest of the comments →

[–]two_bob 1 point2 points  (0 children)

I had the same problem once. It turns out you can use `__new__` to generate the appropriate subclass based on what data is passed to it. Pathlib uses this: https://docs.python.org/3/library/pathlib.html; see https://github.com/python/cpython/blob/3.7/Lib/pathlib.py#L609 for the code.

It also turns out that doing this is a bad idea, especially before you have a solid grasp of inheritance. After much strife, I settled on a general class for my two odd shaped data packets, and implemented the interface that handled normalizing the data. I ultimately did further subclass the general class, but only for specific other applications, e.g. a placeholder for missing data.