property() usage - is this as good as it gets?

castironpi castironpi at gmail.com
Sat Aug 23 20:51:01 EDT 2008


On Aug 22, 11:18 am, David Moss <drk... at gmail.com> wrote:
> Hi,
>
> I want to manage and control access to several important attributes in
> a class and override the behaviour of some of them in various
> subclasses.
>
> Below is a stripped version of how I've implemented this in my current
> bit of work.
>
> 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)

To the OP: you have a unique procedure executed for every attribute of
an instance, for each of set, get, and del.  That's a lot of code.  If
you don't want unique procedures, what patterns should they follow?
What is wrong with the way you overrode 'get_x' above?  When
brainstorming, don't restrict yourself to Python syntax-- make
something up, and we'll write Python.



More information about the Python-list mailing list