4 ms·
Pretty Sweet. Here's my hacked together Python version (not sure what Monads have to do with it): class Maybe: def __init__(self, obj): self.
by aston 19y ago
Pretty Sweet. Here's my hacked together Python version (not sure what Monads have to do with it):
class Maybe:
def __init__(self, obj):
self.o = obj
def __repr__(self):
return repr(self.o)
def __getattr__(self, name):
try:
return Maybe(getattr(self.o,name))
except:
return Maybe(None)
I'm trying to decide whether using it's Pythonic or not.
- bayareaguy 19y agoAre you sure you want to use __repr__ in this way? You'll end up with 'None' instead of None at the end.
- deleted 19y ago[deleted]
- deleted 19y ago[deleted]
- aston 19y agoEh, I don't think the repr matters much. What does matter is actually getting all of the other methods implemented (stuff like cmp, eq, etc.). They're all basically pass-throughs, though. Even with that, I think it's still broken in some ways. I should learn more about what Python does for type conversions.
- deleted 19y ago[deleted]