制作不可变的实例 [英] Making immutable instances
问题描述
Howdy all,
如何(用户定义的)类确保其实例是不可变的,如int或元组,没有继承自那些
类型?
在制作不可变实例时应注意哪些警告?
-
\爱情是想象力胜过智慧的胜利。 - |
` \ Henry L. Mencken |
_o__)|
Ben Finney
Howdy all,
How can a (user-defined) class ensure that its instances are
immutable, like an int or a tuple, without inheriting from those
types?
What caveats should be observed in making immutable instances?
--
\ "Love is the triumph of imagination over intelligence." -- |
`\ Henry L. Mencken |
_o__) |
Ben Finney
推荐答案
Ben Finney写道:
Ben Finney wrote:
(用户定义的)类如何确保其实例不可变,如int或者一个元组,没有继承这些类型?
在制作不可变实例时应该注意哪些警告?
How can a (user-defined) class ensure that its instances are
immutable, like an int or a tuple, without inheriting from those
types?
What caveats should be observed in making immutable instances?
简而言之,你可以' T。我通常更努力从元组派生来实现这个
(定义一些只读属性来通过属性访问项目)。然后需要使用
__slots__来避免人们向实例添加属性。
事实上,我不知道其基本原理。我认为这是一个很好的补充
能够说__immutable__ = True或类似的东西。或者至少,如果有人解释我为什么不能或不应该这样做,我会感激不尽。
-
Giovanni Bajo
In short, you can''t. I usually try harder to derive from tuple to achieve this
(defining a few read-only properties to access item through attributes). Using
__slots__ is then required to avoid people adding attributes to the instance.
In fact, I don''t know the rationale. I would think it would be a great addition
to be able to say __immutable__ = True or something like that. Or at least, I''d
be grateful if someone explained me why this can''t or shouldn''t be done.
--
Giovanni Bajo
" Giovanni Bajo" <无*** @ sorry.com>写道:
"Giovanni Bajo" <no***@sorry.com> writes:
Ben Finney写道:
Ben Finney wrote:
一个(用户定义的)类如何确保其实例是不可变的,如int或元组,没有继承这些类型?
在制作不可变实例时应注意哪些注意事项?
How can a (user-defined) class ensure that its instances are
immutable, like an int or a tuple, without inheriting from those
types?
What caveats should be observed in making immutable instances?
简而言之,你不可能。我通常会更努力地从元组派生来实现这个目标(定义一些只读属性来通过属性访问项目)。然后需要使用
__slots__来避免人们向实例添加属性。
In short, you can''t. I usually try harder to derive from tuple to achieve this
(defining a few read-only properties to access item through attributes). Using
__slots__ is then required to avoid people adding attributes to the instance.
不能再依赖它了。
我很好奇为什么你关心如果人们为你的属性添加属性
" immutable"类。就个人而言,我认为类型的实例
不要让我添加属性作为疣。
< mike
-
Mike Meyer< mw*@mired.org> http://www.mired.org/home/mwm/
独立的WWW / Perforce / FreeBSD / Unix顾问,电子邮件以获取更多信息。
Note that this property of __slots__ is an implementation detail. You
can''t rely on it working in the future.
I''m curious as to why you care if people add attributes to your
"immutable" class. Personally, I consider that instances of types
don''t let me add attributes to be a wart.
<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Mike Meyer< mw*@mired.org>写道:
Mike Meyer <mw*@mired.org> writes:
我很好奇为什么你关心人们如何为你的不可变添加属性?类。就个人而言,我认为类型的实例不允许我添加属性作为疣。
I''m curious as to why you care if people add attributes to your
"immutable" class. Personally, I consider that instances of types
don''t let me add attributes to be a wart.
我同意。对我来说,似乎OP有点想从背面实现
封装。
无论如何,因为他没有提到任何关于使用的内容对于
任何一种优化的不变性,我认为这是关于控制的。
-
Bj ?? rn Lindstr ?? m< bk ** @ stp.lingfil.uu.se>
计算语言学学生,瑞典乌普萨拉大学
I agree. To me it seems like the OP kind of wants to implement
encapsulation from the back way.
Anyway, as he doesn''t mention anything about using this immutability for
any kind of optimisation, I assume it''s about "Control".
--
Bj??rn Lindstr??m <bk**@stp.lingfil.uu.se>
Student of computational linguistics, Uppsala University, Sweden
>
这篇关于制作不可变的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!