TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Python: hash, id and Dictionary Order

9 点作者 Nurdok将近 12 年前

3 条评论

DrJosiah将近 12 年前
Python will generally use the system allocation functions (unless it knows more about the objects and whether they can be bulk allocated and used in freelists), and a given Python class (which is what you are creating) will have alignment that depends on its size and what the platform returns for a block of memory of sufficient size to fit the object. What do I mean?<p>Say that you need to allocate 63 bytes of memory. No memory allocator will give you 63 bytes, you will actually get 64 or maybe more. Why? Because if you allocate 63, then the implication is that some other call to the allocator will return a memory offset just after your 63 bytes, leaving it unaligned WRT platform alignment expectations (which is 4 or 8 byte alignment for platforms I am familiar with).<p>If it so happens that the classes you are building (the A, B, C, ... classes) are aligned on 128 byte boundaries, then it&#x27;s likely that the allocator has put each class into a block size of 128 bytes. What would go into that 128 bytes? Refcounts, reference to the class dictionary (if any), reference to a __slots__ object, the base classes, etc. There is likely unused bytes in the tail end of that 128 byte block, and in some cases, empty space within the block (some 64 bit platforms require 8-byte alignment, even when a structure has 1, 2, or 4 byte integers).<p>But yeah. Don&#x27;t rely on dictionary ordering, and don&#x27;t rely on 128 byte alignment of your classes.
评论 #5982175 未加载
civilian将近 12 年前
Really interesting about the internals of python!<p>One of the first things the professor of my python class told us is that python dictionaries don&#x27;t guarantee any order. So that&#x27;s just always been in mind. It almost seems like a PHP-ism to assume that a dictionary would provide that. Understand thy data-types!<p># edit: And yeah-- I don&#x27;t know why the stackoverflow link was the place to go. I don&#x27;t like &quot;RTFM&quot; culture, but it was right there: <a href="http://docs.python.org/2/tutorial/datastructures.html#dictionaries" rel="nofollow">http:&#x2F;&#x2F;docs.python.org&#x2F;2&#x2F;tutorial&#x2F;datastructures.html#dictio...</a><p>and also here in the book I learned python from: <a href="http://www.greenteapress.com/thinkpython/html/thinkpython012.html" rel="nofollow">http:&#x2F;&#x2F;www.greenteapress.com&#x2F;thinkpython&#x2F;html&#x2F;thinkpython012...</a>
gus_massa将近 12 年前
The analysis about the internal details is interesting, but it&#x27;s not surprising, from <a href="http://docs.python.org/2/library/stdtypes.html#dict.items" rel="nofollow">http:&#x2F;&#x2F;docs.python.org&#x2F;2&#x2F;library&#x2F;stdtypes.html#dict.items</a> :<p><i>&gt; CPython implementation detail: Keys and values are listed in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and deletions.</i>