UWPCE-PythonCert / PythonCertDevel

Development Repo for the Certificate Program
https://uwpce-pythoncert.github.io/PythonCertDevel/index.html
Other
7 stars 15 forks source link

Idea for metaprogramming lesson: #82

Open PythonCHB opened 6 years ago

PythonCHB commented 6 years ago

One of our students did the following properties to normalize the names of the Donors in mailroom.

Could that code repetition be avoided with some meta-programming?

 @property
    def first_name(self):
        """ return the first name """
        return self._first_name

    @first_name.setter
    def first_name(self, value):
        """ set the first name """
        self._first_name = value.title()

    @property
    def middle_name(self):
        """ return the middle name """
        return self._middle_name

    @middle_name.setter
    def middle_name(self, value):
        """ set the middle name """
        self._middle_name = value.title()

    @property
    def last_name(self):
        """ return the last name """
        return self._last_name

    @last_name.setter
    def last_name(self, value):
        """ set the last name """
        self._last_name = value.title()

    @property
    def suffix(self):
        """ return the suffix """
        return self._suffix
PythonCHB commented 6 years ago

Here's his code:

https://github.com/johnnzz/IntroPython-2017/blob/ce7e42ba49cd1c80ef2f865f08a171f375874956/students/johnn/session09/mailroom.py