可变和不可变类型背后的理论是什么? [英] What is the theory behind mutable and immutable types?

查看:96
本文介绍了可变和不可变类型背后的理论是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Python佩服的一件事是它在可变类型和不可变类型之间的区别.在使用Python之前花了一段时间的c编程经验,我惊讶于Python多么容易地摆脱了指针反引用的所有复杂性,这些复杂性使我对c感到恼火.在Python中,一切都按我期望的方式运行,并且我很快意识到,可变/不可变的区别在其中起着重要的作用.

One of the things that I admire about Python is its distinction between mutable and immutable types. Having spent a while programming in c before coming to Python, I was astonished at how easily Python does away with all the complexities of pointer dereferencing that drive me mad in c. In Python everything just works the way I expect, and I quickly realized that the mutable/immutable distinction plays an important part in that.

当然还有一些皱纹(可变函数参数默认值是一个显着的例子),但总的来说,我认为可变/不可变的区别极大地阐明了什么是变量及其值是什么以及他们应该如何表现.

There are still a few wrinkles, of course (mutable function argument defaults being a notable example) but overall, I feel that the mutable/immutable distinction greatly clarifies the question of what variables and their values are and how they ought to behave.

但是它从哪里来?我必须假设GvR不是第一个认识到这种区别的人,并且Python并不是第一个使用它的语言.我有兴趣了解使用此概念的早期语言,以及对此的任何早期理论讨论.

But where does it come from? I have to assume that GvR was not the first person to conceive of this distinction, and that Python was not the first language to use it. I'm interested in hearing about earlier languages that used this concept, as well as any early theoretical discussions of it.

推荐答案

目标C加载了可变/不可变的区别(例如,同时存在NSStringNSMutableString).它比Python早了大约8年. Smalltalk是Objective C继承其大部分OO设计的基础,在较小程度上使用了该概念(值得注意的是,字符串不是不可变的;如今的趋势是使用不可变的字符串,如Python,Ruby等).

Objective C is loaded with mutable/immutable distinctions (to the point where there are both NSString and NSMutableString, for example); it predates Python by about 8 years. Smalltalk, from which Objective C inherited much of its OO design, uses the concept to a lesser extent (notably, strings are not immutable; the trend these days is towards immutable strings as in Python, Ruby, etc.).

这篇关于可变和不可变类型背后的理论是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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