Created
January 7, 2021 16:56
-
-
Save StephenFordham/55caf30f14d607068c606487558ca89a to your computer and use it in GitHub Desktop.
delattr_dunder_method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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