为什么Python决定反对常量引用? [英] Why has Python decided against constant references?

查看:109
本文介绍了为什么Python决定反对常量引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:我不是在谈论防止变量的重新绑定.我说的是防止对变量引用的内存进行修改,以及阻止通过跟踪嵌套容器可以从那里访问的任何内存.

Note: I'm not talking about preventing the rebinding of a variable. I'm talking about preventing the modification of the memory that the variable refers to, and of any memory that can be reached from there by following the nested containers.

我的数据结构很大,我想以只读方式将其公开给其他模块.在Python中执行此操作的唯一方法是深度复制要公开的特定片段-就我而言,这太昂贵了.

I have a large data structure, and I want to expose it to other modules, on a read-only basis. The only way to do that in Python is to deep-copy the particular pieces I'd like to expose - prohibitively expensive in my case.

我确信这是一个非常普遍的问题,而且似乎不断引用将是完美的解决方案.但是我一定想念一些东西.也许常量引用很难在Python中实现.也许他们没有完全按照我的想法做.

I am sure this is a very common problem, and it seems like a constant reference would be the perfect solution. But I must be missing something. Perhaps constant references are hard to implement in Python. Perhaps they don't quite do what I think they do.

任何见解都会受到赞赏.

Any insights would be appreciated.

虽然答案很有用,但我还没有看到const难以在Python中实现或无法使用的单一原因.我想非Pythonic"也可以算作一个正当的理由,但这是真的吗? Python确实对专用实例变量(以__开头)进行了加扰以避免意外的错误,并且const的本质似乎并没有什么不同.

While the answers are helpful, I haven't seen a single reason why const would be either hard to implement or unworkable in Python. I guess "un-Pythonic" would also count as a valid reason, but is it really? Python does do scrambling of private instance variables (starting with __) to avoid accidental bugs, and const doesn't seem to be that different in spirit.

我只是提供了一个非常适度的赏金.我正在寻找有关Python为什么最终不带const的更多详细信息.我怀疑原因是很难实现完美的工作.我想了解为什么这么难.

I just offered a very modest bounty. I am looking for a bit more detail about why Python ended up without const. I suspect the reason is that it's really hard to implement to work perfectly; I would like to understand why it's so hard.

推荐答案

PEP 351中,Barry Warsaw提出了一种冻结"任何可变数据结构的协议,类似于frozenset形成不可变集的方式.冻结的数据结构将是可散列的,因此可以用作字典中的键.

In PEP 351, Barry Warsaw proposed a protocol for "freezing" any mutable data structure, analogous to the way that frozenset makes an immutable set. Frozen data structures would be hashable and so capable being used as keys in dictionaries.

该提案是在python-dev上讨论的,其中最详细地描述了对Raymond Hettinger的批评.

The proposal was discussed on python-dev, with Raymond Hettinger's criticism the most detailed.

这不是您所追求的,但它是我能找到的最接近的东西,应该使您对Python开发人员在该主题上的想法有所了解.

It's not quite what you're after, but it's the closest I can find, and should give you some idea of the thinking of the Python developers on this subject.

这篇关于为什么Python决定反对常量引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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