OOP noob question: Mixin properties
Micky Hulse
mickyhulse.lists at gmail.com
Thu Dec 13 19:20:51 EST 2012
Learning things here...
In my mixin class (a version I wrote for Django) I had this
(indentation removed for better list readability):
<snip>
class JSONResponseMixin(object):
def __init__(self):
self._cache = False
self._cache_timeout = 86400
self._cache_key = None
@property
def cache(self):
return self._cache
@cache.setter
def cache(self, value):
self._cache = value
@cache.deleter
def cache(self):
del self._cache
@property
def cache_timeout(self):
return self._cache_timeout
@cache_timeout.setter
def cache_timeout(self, value):
self._cache_timeout = value
@cache_timeout.deleter
def cache_timeout(self):
del self._cache_timeout
@property
def cache_key(self):
return self._cache_key
@cache_key.setter
def cache_key(self, value):
self._cache_key = value
@cache_key.deleter
def cache_key(self):
del self._cache_key
# ...
</snip>
...which works! But, then I looked here:
<https://github.com/django/django/blob/master/django/views/generic/edit.py#L198>
Simply doing this:
<snip>
class JSONResponseMixin(object):
cache = False
cache_timeout = 86400 # 24 hours.
cache_key = None
# ...
<snip>
...works just as good! Not to mention, it's less verbose.
So, from my view class:
<snip>
class Api(jmix.JSONResponseMixin, BaseDetailView):
# ...
self.cache = True
self.cache_timeout = 864000
self.cache_key = 'foo:bar:baz'
# ...
return self.render_to_response(data)
</snip>
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
M
More information about the Python-list
mailing list