Skip to content

Instantly share code, notes, and snippets.

@StephenFordham
Created January 7, 2021 16:56
Show Gist options
  • Save StephenFordham/55caf30f14d607068c606487558ca89a to your computer and use it in GitHub Desktop.
Save StephenFordham/55caf30f14d607068c606487558ca89a to your computer and use it in GitHub Desktop.
delattr_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
def __delattr__(self, key):
if key == '_name':
raise AttributeError('The name attribute can not be deleted')
else:
del self.__dict__[key]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment