问题描述
您好,
实施getter和setter的Pythonic方法是什么?我听说过
有人说使用访问器不是Pythonic。但为什么?什么是
替代方案?我没有使用它们,因为它们闻到了
Javaish。
但现在我的代码库正在扩展,我开始欣赏它了
背后的智慧。我欢迎示例代码和插图。
问候
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
推荐答案
使用公共成员并在需要时将其转换为属性
Using public members and turning them into properties when needed
因为没有必要随处可见
Because there''s no need to have them everywhere
搜索python property
-
劳伦斯 -
"没有什么比一个想法更危险
如果它是你唯一拥有的那个 - EA Chartier
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
你的意思是什么?一种语言没有属性的智慧
机制因此无法拦截实例成员的设置和获取
需要一个称为JAVA Beans的庞大惯例,所以_all_
属性的使用通过一些代码进行隧道传输,即使只有百分之几的这些实际需要这些?
或通过使用大约
的访问修饰符来扼杀开发人员的智慧。十几种不同的规则对成年人来说是一种烦恼
开发人员至少可以说?
这些是他们不是pythonic的原因。我们可以拦截财产
访问权限(参见属性描述符,,
我们相信开发人员能够自己判断表单是否有问题
代码内部是一个好主意。
问候,
Diez
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
你的意思是什么?一种语言没有属性的智慧
机制因此无法拦截实例成员的设置和获取
需要一个称为JAVA Beans的庞大惯例,所以_all_
属性的使用通过一些代码进行隧道传输,即使只有百分之几的这些实际需要这些?
或通过使用大约
的访问修饰符来扼杀开发人员的智慧。十几种不同的规则对成年人来说是一种烦恼
开发人员至少可以说?
这些是他们不是pythonic的原因。我们可以拦截财产
访问权限(参见属性描述符,,
我们相信开发人员能够自己判断表单是否有问题
代码内部是一个好主意。
问候,
Diez
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
这篇关于Python中的访问器(getter和setter)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!