Skip to content

Instantly share code, notes, and snippets.

@StephenFordham
Last active January 7, 2021 16:39
Show Gist options
  • Save StephenFordham/5667a4149a0b8f4ae2323a5776e5c944 to your computer and use it in GitHub Desktop.
Save StephenFordham/5667a4149a0b8f4ae2323a5776e5c944 to your computer and use it in GitHub Desktop.
setattr_dunder_method
class Employees(object):
def __init__(self, name, age, id_number):
self._name = name
self._age = age
self._id_number = id_number
def __setattr__(self, key, value):
if key == '_name' and hasattr(self, '_name'):
raise AttributeError('The value for the name attribute has already been set, and can not be re-set')
if key in ['_age', '_id_number']:
self.__dict__[key] = int(value)
self.__dict__[key] = value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment