Deeper Understanding Of Python Object Mechanisms
Solution 1:
Immutable atomic objects are singletons
Nope, some are and some aren't, this is a detail of the CPython implementation.
Integers in the range
(-6, 256]
are cached and when a new request for these is made the already existing objects are returned. Numbers outside that range are subject to constant folding where the interpreter re-uses constants during compilation as a slight optimization. This is documented in the section on creating newPyLong
objects.Also, see the following for a discussion on these:
Strings literals are subject to interning during the compilation to bytecode as do ints. The rules for governing this are not as simplistic as for ints, though: Strings under a certain size composed of certain characters are only considered. I am not aware of any section in the docs specifying this, you could take a look at the behavior by reading here.
Floats, for example, which could be considered "atomic" (even though in Python that term doesn't have the meaning you think) there are no singletons:
i = 1.0 j = 1.0 i is j # False
they are still of course subject to constant folding. As you can see by reading: 'is' operator behaves unexpectedly with floats
Immutable and Iterable objects might be singletons but not exactly instead they hash equally
Empty immutables collections are signletons; this is again an implementation detail that can't be found in the Python Reference but truly only discovered if you look at the source.
See here for a look at the implementation: Why does '() is ()' return True when '[] is []' and '{} is {}' return False?
Passing dictionary by double dereferencing works as shallow copy of it.
Yes. Though the term isn't double dereferencing, it is unpacking.
Are those behaviours well documented somewhere?
Those that are considered an implementation detail needn't be documented in the way you'd find documentation for the max
function for example. These are specific things that might easily change if the decision is made so.
Post a Comment for "Deeper Understanding Of Python Object Mechanisms"