Last active
January 7, 2021 16:39
-
-
Save StephenFordham/5667a4149a0b8f4ae2323a5776e5c944 to your computer and use it in GitHub Desktop.
setattr_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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment