Skip to content

Instantly share code, notes, and snippets.

@StephenFordham
Created January 7, 2021 17:17
Show Gist options
  • Save StephenFordham/76089eb26907caf9bd43a49d5046fa26 to your computer and use it in GitHub Desktop.
Save StephenFordham/76089eb26907caf9bd43a49d5046fa26 to your computer and use it in GitHub Desktop.
custom_deletion_exception_method
class DeletionException(Exception):
def __init__(self, original_value):
self._original_value = original_value
def __str__(self):
return 'The value for the name attribute, {} can not be deleted'.format(self._original_value)
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
def __delattr__(self, key):
if key == '_name':
raise DeletionException(self.__dict__['_name'])
else:
del self.__dict__[key]
emp_1 = Employees('stephen', 30, 19242)
del emp_1._name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment