python getter setter_Python中的访问器(getter和setter)

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值