Hello,
What is the Pythonic way of implementing getters and setters. I''ve
heard
people say the use of accessors is not Pythonic. But why? And what is
the alternative? I refrain from using them because they smell
"Javaish."
But now my code base is expanding and I''m beginning to appreciate the
wisdom behind them. I welcome example code and illustrations.
Regards
解决方案mystilleef
What is the Pythonic way of implementing getters and setters.
Using public members and turning them into properties when needed
I''ve
heard
people say the use of accessors is not Pythonic. But why?
Because there''s no need to have them everywhere
But now my code base is expanding and I''m beginning to appreciate the
wisdom behind them. I welcome example code and illustrations.
Search for "python property"
--
Lawrence - http://www.oluyede.org/blog
"Nothing is more dangerous than an idea
if it''s the only one you have" - E. A. Chartier
mystilleef wrote:
Hello,
What is the Pythonic way of implementing getters and setters. I''ve
heard
people say the use of accessors is not Pythonic. But why? And what is
the alternative? I refrain from using them because they smell
"Javaish."
But now my code base is expanding and I''m beginning to appreciate the
wisdom behind them. I welcome example code and illustrations.
Which wisdom do you mean? The wisdom that a language that has no property
mechanism and thus can''t intercept setting and getting of instance members
needs a bulky convention called JAVA Beans, so that _all_ uses of
properties are tunneled through some code, even if only a few percent of
these actually need that?
Or the wisdom that strangling developers by putting access modifiers with
approx. a dozen different rules in place is an annoyance to adult
developers to say the least?
These are the reasons they are not pythonic. We can intercept property
access (see the property descriptor, http://pyref.infogami.com/property),
and we trust in developers being able to judge form themselves if messing
with internals of code is a good idea or not.
Regards,
Diez
I decided to change the name of an attribute. Problem is I''ve used the
attribute in several places spanning thousands of lines of code. If I
had encapsulated the attribute via an accessor, I wouldn''t need to do
an unreliable and tedious search and replace accross several source
code files to achieve my goal. I could simply change the name of the
attribute and move on. Well, I''m glad python has properties. It''s a
feature that should be advertised more, especially for large scale
python development.
Diez B. Roggisch wrote:
mystilleef wrote:
Hello,
What is the Pythonic way of implementing getters and setters. I''ve
heard
people say the use of accessors is not Pythonic. But why? And what is
the alternative? I refrain from using them because they smell
"Javaish."
But now my code base is expanding and I''m beginning to appreciate the
wisdom behind them. I welcome example code and illustrations.
Which wisdom do you mean? The wisdom that a language that has no property
mechanism and thus can''t intercept setting and getting of instance members
needs a bulky convention called JAVA Beans, so that _all_ uses of
properties are tunneled through some code, even if only a few percent of
these actually need that?
Or the wisdom that strangling developers by putting access modifiers with
approx. a dozen different rules in place is an annoyance to adult
developers to say the least?
These are the reasons they are not pythonic. We can intercept property
access (see the property descriptor, http://pyref.infogami.com/property),
and we trust in developers being able to judge form themselves if messing
with internals of code is a good idea or not.
Regards,
Diez