Is there any situation where it makes sense to define a python property, where getting (not setting) it would raise an exception? I don’t think this ever happens for fields in classes, hence the question.
Or is always better in these cases to use a method instead?
9
I’ve created properties that raise exceptions in situations where I didn’t want __init__
to raise exceptions and/or I wanted to delay processing.
class Identifier:
def __init__(self, value):
self._value = value
@property
def prefix(self):
# ...
# parse prefix from self._value
# raise ValueError is provided value cannot be parsed
# ...
return prefix
@property
def suffix(self):
# ...
# parse suffix from self._value
# raise ValueError is provided value cannot be parsed
# ...
return suffix