Python @property: setter, getter

class UserPreference(object):
    DEFAULT_VALUES = {
        'allow_fb_publish': True,
    }

    def __init__(self, user_id):
        """
        pref:2876321
        {
            'allow_fb_publish': 1,
        }
        """

        self.user_id = user_id
        self.key = 'pref:%s' % (self.user_id)

    @property
    def allow_fb_publish(self):
        value = rdb.hget(self.key, 'allow_fb_publish')
        if value is None:
            value = self.allow_fb_publish = self.DEFAULT_VALUES['allow_fb_publish']

        return value

    @allow_fb_publish.setter
    def allow_fb_publish(self, new_value):
        if not isinstance(new_value, bool):
            raise TypeError('Must be bool, not %s' % (type(new_value).__name__))

        new_value_int = int(new_value)
        rdb.hset(self.key, 'allow_fb_publish', new_value_int)

        return new_value_int

ref:
http://www.programiz.com/python-programming/property
http://openhome.cc/Gossip/Python/Property.html