appending to a list via properties

Alex Martelli aleaxit at yahoo.com
Sat Feb 11 20:54:06 EST 2006


Xavier Morel <xavier.morel at masklinn.net> wrote:

> Alex Martelli wrote:
> > Carl Banks <invalidemail at aerojockey.com> wrote:
> >    ...
> >>>     class better_list (list):
> >>>         tail = property(None, list.append)
> >> This is an impressive, spiffy little class.
> > 
> > Yes, nice use of property.
> > 
> > Alex
> 
> I don't know, I usually see people considering that properties are 
> "cool" as long as they don't have side effects, as long as they're 
> virtual members.

If a property didn't have any side effects, there would be no reason to
define it as a property. I do fully expect x.y=z to have "side effects"
on z when y is an assignable property.

> The tail property doesn't behave like member data at all, the semantics
> are strange, counter-intuitive (tail would usually be the end of the 
> list, either [-1] or [1:], in this case it's some magic position at the
> end of the list). And it has one hell of a side effect.

The name 'tail' may not be ideal (for people coming from languages where
'head' is the first element and 'tail' is all the others, in
particular). But the side effect doesn't look hellish at all to me.


Alex



More information about the Python-list mailing list