'How to perform input validation for read-only instance attributes?
A very similar question was posted here, but there are no accepted answers, no code examples and I don't really like the idea of using an external library as suggested by the only one answer provided there.
The following code allows to define read-only instance attributes:
class Point:
def __init__(self, x, y):
self._x = x
self._y = y
@property
def x(self):
return self._x
@property
def y(self):
return self._y
but I would like to validate the user inputs as well. I want to validate if x and y have the right type.
What is the most pythonic/elegant way for doing this? If I provide setters, the attributes are no more read-only.
Is performing the input validation within the constructor the only way to go?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
