其中哪些在 Python 中是不可变的?

发布于 2024-11-01 08:04:11 字数 479 浏览 5 评论 0原文

我试图弄清楚以下内容在Sage中是否是不可变的(它是基于Python构建的,所以我相信如果它在Python中是不可变的,我相信在大多数情况下它在Sage中是不可变的)

下面是对象e,f,g,我

class e: pass
f = e()
g = pi # (g's "type" in Sage is symbolic expression. It's supposed to be 3.1415....etc)
i = lambda x: x*x

认为 e 是一个类,这意味着它是可变的(不可变类有意义吗?所有类都不能修改吗?)。由于 f 是类的实例,我猜测它也是可变的,因为类是可变的。

由于数字在Python中是不可变的,因此g也应该是不可变的,因为尽管它是无理数,但它仍然是一个数字

最后i是一个函数,这意味着它应该是可变的?

我不太确定我是否理解不变性的概念。函数不可变意味着什么?一个类是不可变的吗?

I am trying to figure out whether the following are immutable in Sage (which is built on Python so I believe if it is immutable in python, I believe in most cases it will be immutable in Sage)

Below are objects e, f, g, i

class e: pass
f = e()
g = pi # (g's "type" in Sage is symbolic expression. It's supposed to be 3.1415....etc)
i = lambda x: x*x

I gather that e is a class which means it is mutable (Does an immutable class make sense? Can't all classes be modified?). Since f is an instance of a class, I am guessing it is also mutable since classes are mutable.

Since numbers are immutable in Python, g should be immutable as well since it is a number despite being irrational

Finally i is a function which means it should be mutable?

I'm not quite sure I understand that concept of immutability. What would it mean for a function to be immutable? For a class to be immutable?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水溶 2024-11-08 08:04:11

e 是可变的。例如,您可以在类上添加一个新方法:e.foo = lambda self,x: x

f 是可变的。例如,您可以向此类实例添加一个新字段:fx = 99

g 是不可变的。你无法改变它的任何事情。

i 不是一成不变的。你可以对它做各种邪恶的事情:i.func_code = (lambda x: 123).func_code,之后i(10)将是123而不是100。 (您还可以对其做更合理的事情。在 i.__doc__ = "This function returns the square of its argument." 之后,您将从 help(i)< 获得更有用的结果/code>.)

如果您可以对对象执行某些操作来改变其未来可能的行为,则该对象是可变的。您无法更改 10 的行为;您可以更改函数对象、类、类实例或列表的行为。 (但不是元组。元组一旦创建,只要存在,它就会保持原样。)

e is mutable. You can, for instance, add a new method on the class: e.foo = lambda self,x: x.

f is mutable. You can, for instance, add a new field to this class instance: f.x = 99.

g is immutable. You can't change anything about it.

i is not immutable. You can do all sorts of evil things to it: i.func_code = (lambda x: 123).func_code after which i(10) will be 123 instead of 100. (You can also do more reasonable things to it. After i.__doc__ = "This function returns the square of its argument." you will get a more helpful result from help(i).)

An object is mutable if there's something you can do to the object that changes its possible future behaviour. You can't change the behaviour of 10; you can change the behaviour of a function object, or a class, or a class instance, or a list. (But not a tuple. Once a tuple is made, it stays just as it is for as long as it exists.)

小瓶盖 2024-11-08 08:04:11

正式?如果一个对象可以更改而不更改身份,则该对象是可变的。

列表是可变的,因此特定实例的值可以随着时间的推移而改变:

>>> x = orig_x = []
>>> x == []
True
>>> x += [1]
>>> x == []      # The value of x has changed
False
>>> x is orig_x  # But the identity remains the same
True

但是数字是不可变的,因此它们的值不能改变。相反,必须更新变量以引用完全不同的对象:

>>> x = orig_x = 1
>>> x == 1
True
>>> x += 1
>>> x == 1        # Again, the value of x has changed
False
>>> x is orig_x   # But now the identity has changed as well
False

不变性是一个重要的概念,因为知道对象的值不能更改可以让您对它做出某些假设(例如,dict 实际上需要不可变的键,并且 setfrozenset 需要不可变的成员,因为对象的值会影响它在数据结构中的存储方式(如果允许可变条目),他们可能最终会在如果它们在存储后被修改,则错误的地方)

与流行的看法相反,不覆盖相等定义的用户定义的类在技术上是不可变的。这是因为用户定义类的“值”的默认定义只是id(self)。当一个对象的值就是它的身份时,它们显然不可能随着时间的推移而变化,因此该对象不具有“可变”的性质。

非正式地?大多数人都会凭直觉思考“我可以改变它吗?”沿着 Gareth McCaughan 的回答进行定义。它的基本思想与正式定义相同,只是在平等检查方面使用了比技术定义更广泛的术语“值”含义。

Formally? An object is mutable if it can change value without changing identity.

Lists are mutable, so the value of a particular instance can change over time:

>>> x = orig_x = []
>>> x == []
True
>>> x += [1]
>>> x == []      # The value of x has changed
False
>>> x is orig_x  # But the identity remains the same
True

Numbers are immutable, however, so their value can't change. Instead, the variable has to be updated to refer to a completely different object:

>>> x = orig_x = 1
>>> x == 1
True
>>> x += 1
>>> x == 1        # Again, the value of x has changed
False
>>> x is orig_x   # But now the identity has changed as well
False

Immutability is an important concept, since knowing that an object's value can't change lets you make certain assumptions about it (for example, dict effectively requires immutable keys and set and frozenset require immutable members, as the value of an object affects how it should be stored in the data structure. If mutable entries were permitted, they may end up being in the wrong place if they are modified after being stored)

Contrary to popular belief, user defined classes that don't override the definition of equality are technically immutable. This is because the default definition of the "value" of a user defined class is just id(self). When an object's value is its identity, there is obviously no way for them to differ over time, and hence the object doesn't quality as "mutable".

Informally? Most people use an intuitive "Can I change it?" definition along the lines of Gareth McCaughan's answer. It's the same basic idea as the formal definition, just using a broader meaning of the term "value" than the technical definition in terms of equality checks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文