Upload
codementor
View
5.476
Download
2
Embed Size (px)
Citation preview
How Python internals
affect your code
Martijn Pieters
Abstractions galore
Programming is all about abstractions.
All non-trivial abstractions, to some
degree, are leaky.Joel Spolsky’s Law of Leaky Abstractions
CPython translates your Python code into
machine instructions.
Small integers are singletons
● Everything is an object
● Integers are used all over the place
● Small integers are singletons, there is only
one copy of each small number
● -5 through to 256 are all cached like this
Small integers are singletons
Beginners confuse is with ==Identity is not the same as equality
Use is only for objects that you know to be
singletons, always, like None.
Some strings are interned
Interning: reusing a singleton copy on demand
● All identifiers are interned
● Many string literals are interned
Comparing pointers in C is so much faster than
comparing the contents
Everything is a dictionary -> lots of comparing
When to intern too
Python code can use the intern() function
Use this together with is identity testing
When:
● Large numbers of strings
● Lots of dictionary access or other equality
tests
Peephole optimisations
Peephole optimizer is part of the compiler
● Expressions are simplified
● Some mutable objects are replaced with
immutables
Python will precalculate expressions:
● Numeric calculationsten_days = 10 * 24 * 60 * 60
● Sequencesdefault_retval = (None,) * 5taunt = 'neener' * 3
Expressions are simplified
Mutables replaced by immutables
Membership tests against a literal:if foo in {'ham', 'spam', 'eggs'}
The set() becomes a frozenset()
Introspection and Dissasembly
Demo of object attributes and the dis module