Categories: links, linux, programming, python, snark, solaris, spam, sysadmin, tech, unix, web.
|
2011-11-25 Python instance dictionaries, attribute names, and memory useIn a comment on my entry on what __slots__ are good for, Max wrote:
Although one might naturally think that this is the case, CPython is actually sufficiently clever that it is not so; using __slots__ doesn't save you any memory for attribute names because the string values of attribute names are already only stored once. However understanding how and why requires a reasonable amount of knowledge about CPython internals. (Or you have to know to look at the documentation for the Like many similar languages, Python has string interning and the CPython internals make liberal use of interned strings for any code-related string that might look like it's going to be repeated. Attribute names are one such example of this; starting right in the code itself, all attribute names are fully interned. So you always have the same set of interned strings for attribute names regardless of how the attributes are stored and regardless of how many instances of the class you have. (This is quite similar to part of the concept of 'symbols' in languages like Lisp and Ruby, although both of those expose symbols directly to user-level code.) More specifically, all names used directly as attributes are interned. There are a number of ways where you can use real strings as attribute names and these will not be interned. The most prominent example is actually __slots__ itself, although things get confusing here. Consider:
The two string literals in __slots__ are not interned. However, the same
string value ('attrone') is interned in (Note that attribute names are interned globally, not on a
per-class basis or the like. The 'attrone' in the attribute name
An even more complicated example can be had with ' (In theory direct manipulation of If you are testing this, note that all single-character strings are interned for you; you need to use multi-character attribute names to avoid false positives. (This is undoubtedly far more about this issue than most people want to know. I'm peculiar that way; I can't resist peeking under the hood.) Sidebar: interned versus non-interned versions of a string valueIn some languages, once you intern a string value all future occurrences of that string value, anywhere, are automatically converted to the interned version. CPython doesn't work this way; instead, something has to explicitly convert a string value into an interned version of it and otherwise string values are left alone. It's thus entirely possible, even easy, to have an interned version of a string value as well as one or more non-interned versions of it. (One comment.)
python/InstanceStringUsage written at 00:11:49; Add Comment
|
These are my WanderingThoughts GettingAround This is part of CSpace, and is written by ChrisSiebenmann. * * * Atom feeds are available; see the bottom of most pages. Categories: links, linux, programming, python, snark, solaris, spam, sysadmin, tech, unix, web |