Last active
July 6, 2020 11:52
-
-
Save StephenFordham/0181e32f4c857f3cf533e48bd5fb5ade to your computer and use it in GitHub Desktop.
employees_instance
This file contains 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
# 1. using dunder __setattr__, __delattr__, | |
class Employees(object): | |
def __init__(self, name, age, service_length): | |
self._name = name | |
self._age = age | |
self._service_length = service_length | |
def __setattr__(self, key, value): | |
if key == '_name' and hasattr(self, '_name'): | |
raise AttributeError('This attribute can only be set once in the init method') | |
elif key == '_name': | |
self.__dict__[key] = value.title() | |
else: | |
for key in ['_age', '_service_length']: | |
if not isinstance(value, int): | |
raise TypeError | |
self.__dict__[key] = value | |
def __delattr__(self, key): | |
if key == '_name': | |
raise AttributeError('This attribute cannot be deleted') | |
else: | |
del self.__dict__[key] | |
emp = Employees('stephen', 29, 5) | |
# emp._name = 'new name' | |
# print(emp.__dict__) | |
# for type_check in emp.__dict__: | |
# print(type(emp.__dict__[type_check])) | |
del emp._name | |
# del emp._age | |
# print(emp.__dict__) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment