OOP noob question: Mixin properties

Micky Hulse mickyhulse.lists at gmail.com
Fri Dec 14 01:20:51 CET 2012

Learning things here...

In my mixin class (a version I wrote for Django) I had this
(indentation removed for better list readability):


class JSONResponseMixin(object):

def __init__(self):
self._cache = False
self._cache_timeout = 86400
self._cache_key = None

def cache(self):
    return self._cache

def cache(self, value):
    self._cache = value

def cache(self):
    del self._cache

def cache_timeout(self):
    return self._cache_timeout

def cache_timeout(self, value):
    self._cache_timeout = value

def cache_timeout(self):
    del self._cache_timeout

def cache_key(self):
    return self._cache_key

def cache_key(self, value):
    self._cache_key = value

def cache_key(self):
    del self._cache_key

# ...


...which works! But, then I looked here:


Simply doing this:


class JSONResponseMixin(object):

cache = False
cache_timeout = 86400 # 24 hours.
cache_key = None

# ...


...works just as good! Not to mention, it's less verbose.

So, from my view class:


class Api(jmix.JSONResponseMixin, BaseDetailView):

# ...

self.cache = True
self.cache_timeout = 864000
self.cache_key = 'foo:bar:baz'

# ...

return self.render_to_response(data)


I guess I wanted to run my ideas by the Python gurus just to make sure
I was not doing crazy stuff here...

The above seems better than taking the more functional approach:

return self.render_to_response(data, cache, cache_timeout, cache_key)

Then again, maybe the more functional approach is better for its simplicity?

Hopefully I'm not boring ya'll to death. :D


More information about the Python-list mailing list