你使用 get/set 模式(在 Python 中)吗?
使用 get/set 似乎是 Java 中的常见做法(出于各种原因),但我几乎没有看到使用此方法的 Python 代码。
为什么在 Python 中使用或避免使用 get/set 方法?
Using get/set seems to be a common practice in Java (for various reasons), but I hardly see Python code that uses this.
Why do you use or avoid get/set methods in Python?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
在 python 中,您可以直接访问该属性,因为它是公共的:
如果您想对属性的访问或更改执行某些操作,可以使用 properties:
至关重要的是,客户端代码保持不变。
In python, you can just access the attribute directly because it is public:
If you want to do something on access or mutation of the attribute, you can use properties:
Crucially, the client code remains the same.
很酷的链接:Python 不是 Java :)
Cool link: Python is not Java :)
以下是 Guido van Rossum 在编程大师< /a>
Here is what Guido van Rossum says about that in Masterminds of Programming
不,它不是Pythonic的。普遍接受的方法是使用普通数据属性,并用属性替换需要更复杂的获取/设置逻辑的属性。
No, it's unpythonic. The generally accepted way is to use normal data attribute and replace the ones that need more complex get/set logic with properties.
对您问题的简短回答是否定的,您应该在需要时使用属性。 Ryan Tamyoko 在他的文章 Getters/Setters/Fuxors 中提供了详细的答案
The short answer to your question is no, you should use properties when needed. Ryan Tamyoko provides the long answer in his article Getters/Setters/Fuxors
你的观察是正确的。这不是正常的 Python 编程风格。属性都是公共的,因此您只需访问(获取、设置、删除)它们,就像访问具有它们的任何对象(不仅仅是类或实例)的属性一样。很容易看出 Java 程序员何时学习 Python,因为他们的 Python 代码看起来就像使用 Python 语法的 Java!
我绝对同意之前所有的发帖者,特别是 @Maximiliano 的 Phillip 著名文章的链接和 @Max 的建议,即任何比设置(和获取)类和实例属性的标准方法更复杂的方法都是使用 Properties (或描述符(进一步概括)来自定义属性的获取和设置! (这包括能够添加您自己的自定义版本的私有、受保护、朋友或任何您想要的策略,如果您想要公共以外的东西。)
作为一个有趣的演示,在 核心 Python 编程(第 13 章,第 13.16 节),我想出了一个使用描述符将属性存储到磁盘而不是内存中的示例!是的,这是一种奇怪的持久存储形式,但它确实向您展示了可能性的示例!
这是另一篇相关文章,您可能会发现它也很有用:
Python:多个属性,一个 setter/getter
Your observation is correct. This is not a normal style of Python programming. Attributes are all public, so you just access (get, set, delete) them as you would with attributes of any object that has them (not just classes or instances). It's easy to tell when Java programmers learn Python because their Python code looks like Java using Python syntax!
I definitely agree with all previous posters, especially @Maximiliano's link to Phillip's famous article and @Max's suggestion that anything more complex than the standard way of setting (and getting) class and instance attributes is to use Properties (or Descriptors to generalize even more) to customize the getting and setting of attributes! (This includes being able to add your own customized versions of private, protected, friend, or whatever policy you want if you desire something other than public.)
As an interesting demo, in Core Python Programming (chapter 13, section 13.16), I came up with an example of using descriptors to store attributes to disk instead of in memory!! Yes, it's an odd form of persistent storage, but it does show you an example of what is possible!
Here's another related post that you may find useful as well:
Python: multiple properties, one setter/getter
我来这里就是为了这个答案(不幸的是我不能)。但我在其他地方找到了解决方法。下面的代码可以替代 get 。
类 get_var_lis:
def __init__(自身):
经过
def __call__(自我):
返回 [2,3,4]
def __iter__(自身):
返回迭代器([2,3,4])
some_other_var = get_var_lis
这只是一个解决方法。通过使用上述概念,您也可以轻松地在 py 中构建 get/set 方法。
I had come here for that answer(unfortunately i couldn't) . But i found a work around else where . This below code could be alternative for get .
class get_var_lis:
def __init__(self):
pass
def __call__(self):
return [2,3,4]
def __iter__(self):
return iter([2,3,4])
some_other_var = get_var_lis
This is just a workaround . By using the above concept u could easily build get/set methodology in py too.
我们的老师在课堂上展示了一个例子,解释我们何时应该使用访问器函数。
Our teacher showed one example on class explaining when we should use accessor functions.