Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StephenFordham/6949253800d23b1563943bbda29142dc to your computer and use it in GitHub Desktop.
Save StephenFordham/6949253800d23b1563943bbda29142dc to your computer and use it in GitHub Desktop.
Example_2_using_classes
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