python getter decorator

Solutions on MaxInterview for python getter decorator by the best coders in the world

showing results for - "python getter decorator"
Louise
15 Nov 2018
1class Person(object):
2    def __init__(self, p_name=None):
3        self.name = p_name
4
5    @property
6    def name(self):
7        return self._name
8
9    @name.setter
10    def name(self, new_name):
11        if type(new_name) == str: #type checking for name property
12            self._name = new_name
13        else:
14            raise Exception("Invalid value for name")
15