Properties and private attributes, vs. just regular attributes

View: New views
3 Messages — Rating Filter:   Alert me  

Properties and private attributes, vs. just regular attributes

by Doug Blank-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Benny,

I see that in the plugin objects that you have things like:

   ...
        self._fname = None

    def _set_fname(self, fname):
        self._fname = fname

    def _get_fname(self):
        return self._fname

    fname = property(_get_fname, _set_fname)

I see no advantage of doing all of that over just having an attribute.
Why do this?

-Doug

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Gramps-devel mailing list
Gramps-devel@...
https://lists.sourceforge.net/lists/listinfo/gramps-devel

Re: Properties and private attributes, vs. just regular attributes

by Benny Malengier :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2009/11/5 Doug Blank <doug.blank@...>:

> Benny,
>
> I see that in the plugin objects that you have things like:
>
>   ...
>        self._fname = None
>
>    def _set_fname(self, fname):
>        self._fname = fname
>
>    def _get_fname(self):
>        return self._fname
>
>    fname = property(_get_fname, _set_fname)
>
> I see no advantage of doing all of that over just having an attribute.
> Why do this?

One way to do all of them.
For fname, I cannot think of a reason some plugin would not have it,
but as you can see in the others, this construct is used for error
checking in the function on assign. We could do with fname also, is
the file ended by '.py', does it exist, ...,
and raise error on problem.
So the register code can not just assign whetever it wants to a
plugindata object, everything that is assigned goes through a
function, allowing for control on what the plugin wants to register.

Anyway, the suggestion comes from Richard, I think he is used to doing
it like that. Working with slots you could now optimize the class
object I think.

Note that I don't want to have extra attributes created on the object
outside of what is defined in the init (as your old gramplet register
code could do), so just assigning should raise an error, which in the
register function is taken care of by the gettattr call, this will
give an error if the attribute was not created in the init method of
plugindata.

Benny

>
> -Doug
>
> ------------------------------------------------------------------------------
> Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
> trial. Simplify your report design, integration and deployment - and focus on
> what you do best, core application coding. Discover what's new with
> Crystal Reports now.  http://p.sf.net/sfu/bobj-july
> _______________________________________________
> Gramps-devel mailing list
> Gramps-devel@...
> https://lists.sourceforge.net/lists/listinfo/gramps-devel
>

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Gramps-devel mailing list
Gramps-devel@...
https://lists.sourceforge.net/lists/listinfo/gramps-devel

Re: Properties and private attributes, vs. just regular attributes

by Doug Blank-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, Nov 5, 2009 at 7:12 AM, Benny Malengier
<benny.malengier@...> wrote:

> 2009/11/5 Doug Blank <doug.blank@...>:
>> Benny,
>>
>> I see that in the plugin objects that you have things like:
>>
>>   ...
>>        self._fname = None
>>
>>    def _set_fname(self, fname):
>>        self._fname = fname
>>
>>    def _get_fname(self):
>>        return self._fname
>>
>>    fname = property(_get_fname, _set_fname)
>>
>> I see no advantage of doing all of that over just having an attribute.
>> Why do this?
>
> One way to do all of them.
> For fname, I cannot think of a reason some plugin would not have it,
> but as you can see in the others, this construct is used for error
> checking in the function on assign. We could do with fname also, is
> the file ended by '.py', does it exist, ...,
> and raise error on problem.
> So the register code can not just assign whetever it wants to a
> plugindata object, everything that is assigned goes through a
> function, allowing for control on what the plugin wants to register.
>
> Anyway, the suggestion comes from Richard, I think he is used to doing
> it like that. Working with slots you could now optimize the class
> object I think.
>
> Note that I don't want to have extra attributes created on the object
> outside of what is defined in the init (as your old gramplet register
> code could do), so just assigning should raise an error, which in the
> register function is taken care of by the gettattr call, this will
> give an error if the attribute was not created in the init method of
> plugindata.

Ok, thanks for the explanation. I'm not a big fan of doing this kind
of checking (types, proper attributes, etc.) but I am a fan of unit
tests. I think the test for getattr is a good idea. (As an aside, this
property philosophy would seem to argue for the register function,
don't you think?)

In any event, I've added fname as a property for all plugins and
help_url property for gramplets.

-Doug

> Benny
>
>>
>> -Doug
>>
>> ------------------------------------------------------------------------------
>> Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
>> trial. Simplify your report design, integration and deployment - and focus on
>> what you do best, core application coding. Discover what's new with
>> Crystal Reports now.  http://p.sf.net/sfu/bobj-july
>> _______________________________________________
>> Gramps-devel mailing list
>> Gramps-devel@...
>> https://lists.sourceforge.net/lists/listinfo/gramps-devel
>>
>

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Gramps-devel mailing list
Gramps-devel@...
https://lists.sourceforge.net/lists/listinfo/gramps-devel