properties and get/set methods
John Roth
johnroth at ameritech.net
Sun Apr 6 08:58:32 EDT 2003
"Mike C. Fletcher" <mcfletch at rogers.com> wrote in message
news:mailman.1049604615.22077.python-list at python.org...
> Dan Bishop wrote:
>
> >Timothy Grant <tjg at craigelachie.org> wrote in message
news:<mailman.1049581810.25706.python-list at python.org>...
> >
> >
> ...
>
> >>Is it possible to determine which attribute was used to as the
trigger for the
> >>call to setvar() or getvar()? so that set_appropriate_attribute() or
> >>appropriate_attribute() can set the correct attribute or get the
correct
> >>attribute?
> >>
> >>
> >
> >Why not just use two different getvar and setvar functions?
> >
>
> When you're doing generic "framework" level programming, it's just not
> acceptable to have to define 2 (really 3) new functions for every
> property. Compare:
>
> def setMyProperty( self, value ):
> do_the_generic_thing( self, 'myProperty', value)
> self.__dict__["myProperty"] = value
> def getMyProperty( self ):
> do_the_generic_thing( self, 'myProperty', value)
> return self.__dict__["myProperty"]
> def delMyProperty( self ):
> del self.__dict__[ "myProperty" ]
> myProperty = property( getMyProperty, setMyProperty,
delMyProperty,
> """Some documentation""")
>
> versus:
>
> myProperty = StringProperty(
> "myProperty", """Some documentation""",
> defaultValue = "this value",
> )
>
> In these cases, you're using properties to simplify the modelling of
> domain objects, or to provide some *generic* functionality over and
> above a regular value get/set. You don't want to have to specify that
> functionality for every property, you just want to say "this is one of
> _those_ properties". The code bloat (not to mention mindless
> duplication of code) when you get up to 15 or 20 properties for an
> object would be ridiculous.
Well, I suppose since this is Sunday, it's appropriate to bring
religion into it.
John Roth
>
>
> Enjoy,
> Mike
More information about the Python-list
mailing list