Created
February 12, 2022 18:49
-
-
Save StephenFordham/6949253800d23b1563943bbda29142dc to your computer and use it in GitHub Desktop.
Example_2_using_classes
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
class StrAttrValidation(object): | |
def __init__(self): | |
self._dict = dict() | |
def __set__(self, instance, value): | |
if not isinstance(value, str): | |
raise AttributeError('Only Str type valid') | |
self._dict[instance] = value.title() | |
def __get__(self, instance, value): | |
return self._dict[instance] | |
class Employees(object): | |
name = StrAttrValidation() | |
location = StrAttrValidation() | |
def __init__(self, name, location): | |
self.name = name | |
self.location = location | |
emp1 = Employees('stephen', 689698) | |
print(emp1.name) | |
print(emp1.location) | |
# Terminal Output | |
# File "...." line 63, in __set__ | |
# raise AttributeError('Only Str type valid') | |
# AttributeError: Only Str type valid |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment