I'm writing a library that parses a file, creates an object that represents the file, and allows exporting the object back to a file.
I want to validate that the required headers and columns are included any time those values are changed. Due to this, I was trying to setup validation with the @property decorator.
I noticed in the python documentation for @property they use '_variable' if the property name was 'variable'. I understand that a single underscore in front is to signify the variable is intended for weak internal use. However, I was under the impression the point of the @property decorator was that any call to set a variable would cause the setter function to run.
_headers = None
required_headers = ['FIELD_DELIM', 'VIDEO_FORMAT', 'FPS']
@property
def headers(self):
return self._headers
@headers.setter
def headers(self, value):
for header in self.required_headers:
if header not in value:
raise Exception
self._headers = value
While this code works, I know that I can still bypass my setter by doing myObject._headers=value.
Is there a way I can ensure that validation is always performed without relying on a user to respect _headers is for internal use?