__setattr__与__slots__在Python中约束属性创建 [英] __setattr__ versus __slots__ for constraining attribute creation in Python

查看:140
本文介绍了__setattr__与__slots__在Python中约束属性创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读有关如何使Python类少动,特别是通过不允许用户动态创建新的属性。我读过 overloadding __ setattr__ 是要做到这一点一个很好的方式,和 __ __插槽是不是要走的方式。在这最后一个线程一个后实际上表明, __ __插槽可以打破酸洗。 (任何人都可以证实这一点?)

I've been reading about how make Python classes less dynamic, specifically by not allowing users to dynamically create new attributes. I've read that overloadding __setattr__ is a good way to do this , and __slots__ is not the way to go. One post on this last thread actually suggests that __slots__ can break pickling. (Can anyone confirm this?)

不过,我只是读了whatsnew为Python 2.2和的属性居然建议使用 __ __插槽用于约束属性创建的根本目的,不仅仅是为了与其他人一样的优化建议访问部分。在Python的历史来看,没有人知道是什么的初衷__ __插槽是?是制约变量建立一个功能或错误被滥用?如何才有人看到 __ __插槽应用于实践?有很多人看过 __ __的setattr 超载限制属性创造?哪一个是最好的?如果你比较熟悉的一种方法或其他,随意张贴您知道方法的利弊。另外,如果你有解决问题的方式不一样,请分享! (并请尽量不要只是重复的弊端__ __插槽已经在其他线程的。)

However, I was just reading the whatsnew for Python 2.2, and the attribute access section actually suggests using __slots__ for the very purpose of constraining attribute creation, not just for optimization like others have suggested. In terms of Python history, does anyone know what the original intention of __slots__ was? Is constraining variable creation a feature or a bug to be abused? How have people seen __slots__ used in practice? Have many people seen __setattr__ overloaded to restrict attribute creation? Which one is best? If you are more familiar with one method or the other, feel free to post the pros and cons of the method you know. Also, if you have a different way of solving the problem, please share! (And please try not to just repeat the downsides of __slots__ that have been expressed in other threads.)

编辑:我希望避免讨论为什么?,但第一个答案表示这是要来了,所以我在这里声明它。在有问题的项目中,我们使用这些类存储配置信息,允许用户与他们(用户的)参数的对象设置属性,然后传递对象关闭的程序的其他部分。该物体做的不仅仅是存储参数比较多,所以一本字典是行不通的。我们已经有用户不小心输入属性名称错误,并最终创建一个新的属性,而不是设置该程序期望的属性之一。这在不知不觉中,因此用户认为他们设置参数,但并没有看到预期的结果。这是用户容易混淆,而且很难发现。通过约束属性的创作,一个会抛出异常,而不是默默地传递。

I was hoping to avoid discussion of "why?", but the first answer indicates that this is going to come up, so I'll state it here. In the project in question, we're using these classes to store "configuration information", allowing the user to set attributes on the objects with their (the users') parameters, and then pass the objects off to another part of the program. The objects do more than just store parameters, so a dictionary wouldn't work. We've already had users accidentally type an attribute name wrong, and end up creating a new attribute rather than setting one of the attributes that the program expects. This goes undetected, and so the user thinks they're setting the parameter but doesn't see the expected result. This is confusing for the user, and hard to spot. By constraining attribute creation, an exception will be thrown rather than pass by silently.

编辑2,重新酸洗:将这些对象的东西,我们希望在未来的存储,和酸洗似乎是一个好办法做到这一点。如果 __插槽__ 显然是最好的解决方案,我们也许可以找到另一种方式来存储它们,但酸洗肯定是有价值的,并应保持在考虑。

EDIT 2, re pickling: These objects will be something that we will want to store in the future, and pickling seems like a good way to do this. If __slots__ is clearly the best solution, we could probably find another way to store them, but pickling would definitely be of value, and should be kept in consideration.

编辑3:我还要提到的是节约内存是不是一个问题。只有极少数这些对象将被创建,所以保存所有内存都可以忽略不计(如3-12 GB的机器上千字节10秒)。

EDIT 3: I should also mention that memory conservation isn't an issue. Very few of these objects will be created, so any memory saved will be negligible (like 10s of kilobytes on a 3-12 GB machine).

推荐答案

我对你的使用情况的建议是使用 __ SETATTR __ 并出具的警告在使用Python的警告模块无法识别的属性名称。

My suggestion for your use case is to use __setattr__ and issue a warning when the attribute name is not recognized using Python's warnings module.

这篇关于__setattr__与__slots__在Python中约束属性创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆