pythonic use of properties?

Marcus Goldfish magoldfish at gmail.com
Fri Apr 15 10:41:14 EDT 2005


On 4/15/05, Michael Spencer <mahs at telcopartners.com> wrote:
> > class SillyDecimal(object):
> >    """A silly class to represent an integer from 0 - 99."""
> >    def __init__(self, arg=17):
> >        if isinstance(arg, tuple):
> >            self.tens = arg[0]
> >            self.ones = arg[1]
> It is conventional to indicate 'private' attributes with the _ prefix.
Well, I actually want to expose tens and ones, which is why they are
not private.

> By this standard, you have three 'public' interfaces: number, get/setNumber and
> ones/tens, which is confusing and error-prone.  Moreover, if you are going to
> validate number, it might make more sense to put all the validation logic into
> the setter vs. splitting some into __init__.  So your class could look like:
Of course the toy example would be better coded by changing the
internal representation to number (instead of tens and ones), but then
the point about property validation is lost, as you demonstrate in
your example.

> As for whether it is appropriate to validate the value at all, I think that
> depends on your larger design.
So what do you consider when making this decision, and do these
factors differ between python and C#/Java?

Marcus



More information about the Python-list mailing list