3 ms·
I'm really curious, I write a lot of python, and find myself wishing for sum types all the time. How do you see inheritance filling the same space? I'd love to
by Wilduck 9y ago
I'm really curious, I write a lot of python, and find myself wishing for sum types all the time. How do you see inheritance filling the same space? I'd love to be able to do something like:
data Choices = Good | Bad | Ugly
like I can do in haskell, where a value of type `Choices` can only ever be one of those options. But there doesn't seem to be any reasonable approximation in python.
- Ded7xSEoPKYNsDd 9y agoThe OOP pattern dfox is referring to would look something like this: class AbstractChoice: # defines all common operations on choices # plus potentially some useful stuff on top of those pass class Good(AbstractChoice): # conatins the code specific to good things pass class Bad(AbstractChoice): pass class Ugly(AbstractChoice): pass This isn't a straight-on replacement though. It's especially bad when you want to separate your concerns not along the good-bad-ugly-axis but something different (which is when you'd e.g. go on to use mixins, or that ugly visitor pattern we've seen in the OP).