不变性 [英] Immutability

查看:64
本文介绍了不变性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我读它的方式,Python只允许值(因此类型)

是不可变的,而不是类成员。最接近

后者的方法是使用名称隐藏约定。


这是正确的吗?

问候,

Nick Maclaren。


The way that I read it, Python allows only values (and hence types)
to be immutable, and not class members. The nearest approach to the
latter is to use the name hiding conventions.

Is that correct?
Regards,
Nick Maclaren.

推荐答案

Nick Maclaren写道:
Nick Maclaren wrote:
我读它的方式,Python只允许值(因此类型)
是不可变的,而不是类成员。最接近
后者的方法是使用名称隐藏约定。

这是正确的吗?
The way that I read it, Python allows only values (and hence types)
to be immutable, and not class members. The nearest approach to the
latter is to use the name hiding conventions.

Is that correct?




你也可以制作不允许写作的属性。


类Foo(对象):


def __init __(self,bar):

self._bar = bar


@property

def bar(self):

返回self._bar


-

Robert Kern


我开始相信整个世界都是一个谜,一个无害的谜团

由于我们疯狂地试图解释它而变得可怕,好像它有一个潜在的真相。

- Umberto Eco



You can also make properties that don''t allow writing.

class Foo(object):

def __init__(self, bar):
self._bar = bar

@property
def bar(self):
return self._bar

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco




文章< ma ************** *************************@蟒蛇。 org>,

Robert Kern< ro ********* @ gmail.com>写道:

|> Nick Maclaren写道:

|> >我读它的方式,Python只允许值(因此类型)

|> >是不可改变的,而不是班级成员。最近的方法

|> >后者是使用名称隐藏约定。

|> >

|> >这是正确的吗?

|>

|>您还可以制作不允许写作的属性。

|>

|> class Foo(对象):

|>

|> def __init __(self,bar):

|> self._bar = bar

|>

|> @property

|> def bar(self):

|>返回self._bar


非常感谢。而且,更重要的是,我甚至找到了它的文档!

Whatsnew2.2。 2.4.2参考是,呃,没有用。


Python的一个不太可爱的属性是其

文档的不可理解性:-(


但你知道....

问候,

Nick Maclaren。

In article <ma***************************************@python. org>,
Robert Kern <ro*********@gmail.com> writes:
|> Nick Maclaren wrote:
|> > The way that I read it, Python allows only values (and hence types)
|> > to be immutable, and not class members. The nearest approach to the
|> > latter is to use the name hiding conventions.
|> >
|> > Is that correct?
|>
|> You can also make properties that don''t allow writing.
|>
|> class Foo(object):
|>
|> def __init__(self, bar):
|> self._bar = bar
|>
|> @property
|> def bar(self):
|> return self._bar

Thanks very much. And, what''s more, I have even found its documentation!
Whatsnew2.2. The 2.4.2 reference is, er, unhelpful.

One of Python''s less-lovable attributes is the inscrutability of its
documentation :-(

But you knew that ....
Regards,
Nick Maclaren.


Nick Maclaren写道:
Nick Maclaren wrote:
在文章< ma *************************** ************ @ python。org>,
Robert Kern< ro ********* @ gmail.com>写道:
|> Nick Maclaren写道:
|>>我读它的方式,Python只允许值(因此类型)
|>>是不可变的,而不是类成员。最近的方法
|>>后者是使用名称隐藏约定。
|>>
|>>这是正确的吗?
|>
|>您还可以制作不允许写作的属性。
|>
|> class Foo(对象):
|>
|> def __init __(self,bar):
|> self._bar = ba r
|>
|> @property
|> def bar(self):
|>返回self._bar

非常感谢。而且,更重要的是,我甚至找到了它的文档!
Whatsnew2.2。 2.4.2参考是,呃,没有用。
In article <ma***************************************@python. org>,
Robert Kern <ro*********@gmail.com> writes:
|> Nick Maclaren wrote:
|> > The way that I read it, Python allows only values (and hence types)
|> > to be immutable, and not class members. The nearest approach to the
|> > latter is to use the name hiding conventions.
|> >
|> > Is that correct?
|>
|> You can also make properties that don''t allow writing.
|>
|> class Foo(object):
|>
|> def __init__(self, bar):
|> self._bar = bar
|>
|> @property
|> def bar(self):
|> return self._bar

Thanks very much. And, what''s more, I have even found its documentation!
Whatsnew2.2. The 2.4.2 reference is, er, unhelpful.




是吗?

http://docs.python.org/lib/built-in-funcs.html


文件" property"很好。


Georg



Is it?

http://docs.python.org/lib/built-in-funcs.html

documents "property" quite well.

Georg


这篇关于不变性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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