Styled Text Control - Properties vs. Functions

25 views
Skip to first unread message

Hubert Mayr

unread,
Jun 13, 2016, 9:06:28 AM6/13/16
to wxPython-users
Hello,

E.g. the styled text control has a property Text to either retrieve or set the text 
in the current control but some could also use the functions GetText() or SetText(text)
to do the same.

Is there some advantage/disadvantage using functions instead of properties
or is it just there to satisfy developer preferences?

Hm, while writing these lines I might see an advantage using properties instead of functions.
When the function itself changes, let's say an additional parameter has been added,
and this change can be handled by the framework itself there is no need for changing the property.

But on the other hand if using a property it might be slower in code execution than using the function.

Some insights?

Thank you

Hubert

Tim Roberts

unread,
Jun 13, 2016, 1:36:52 PM6/13/16
to wxpytho...@googlegroups.com
Hubert Mayr wrote:

E.g. the styled text control has a property Text to either retrieve or set the text 
in the current control but some could also use the functions GetText() or SetText(text)
to do the same.

Is there some advantage/disadvantage using functions instead of properties
or is it just there to satisfy developer preferences?

Personal preference.  Properties are called "syntactic sugar", in that they do not provide any added functionality; they just let you write things in a way that might seem more natural.  When you use the Text property, you are actually calling the GetText and SetText methods.  It's just being hidden from you.




Hm, while writing these lines I might see an advantage using properties instead of functions.
When the function itself changes, let's say an additional parameter has been added,
and this change can be handled by the framework itself there is no need for changing the property.

Not really.  If the developer decides that GetText needs a parameter, then to avoid messing with existing code, he's going to provide a default for the new parameter.  As I said, when you do this:
    xxx = std.Text
you are actually calling
    xxx = std.GetText()

If GetText changes, then the property no longer works.



But on the other hand if using a property it might be slower in code execution than using the function.

Any speed difference is absolutely trivial.
-- 
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

Hubert Mayr

unread,
Jun 13, 2016, 7:05:52 PM6/13/16
to wxPython-users
Again, thank you very much.


On Monday, June 13, 2016 at 7:36:52 PM UTC+2, Tim Roberts wrote:

Hm, while writing these lines I might see an advantage using properties instead of functions.
When the function itself changes, let's say an additional parameter has been added,
and this change can be handled by the framework itself there is no need for changing the property.

Not really.  If the developer decides that GetText needs a parameter, then to avoid messing with existing code, he's going to provide a default for the new parameter.  As I said, when you do this:
    xxx = std.Text
you are actually calling
    xxx = std.GetText()

If GetText changes, then the property no longer works.

Of course, why should one do the work twice - should have thought about it.

Thank you

Hubert 
Reply all
Reply to author
Forward
0 new messages