property() usage - is this as good as it gets?
Raymond Hettinger
python at rcn.com
Sun Aug 24 06:14:07 EDT 2008
On Aug 22, 1:18 pm, David Moss <drk... at gmail.com> wrote:
> It works well enough, but I can't help feeling there a cleaner more
> readable way of doing this (with less duplication, etc).
>
> Is this as good as it gets or can this be refined and improved
> especially if I was to add in a couple more attributes some fairly
> complex over-ride logic?
>
> #!/usr/bin/env python
>
> class A(object):
> def __init__(self):
> self._x = None
> self._y = None
>
> def _set_x(self, value):
> self._x = value
>
> def _get_x(self):
> return self._x
>
> x = property(_get_x, _set_x)
>
> def _set_y(self, value):
> self._y = value
>
> def _get_y(self):
> return self._y
>
> y = property(_get_y, _set_y)
FWIW, there is a variant of property() that allows you to re-use the
same getter/setter code for multiple attributes:
http://code.activestate.com/recipes/205126/
Raymond Hettinger
More information about the Python-list
mailing list