Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.
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 S...
Small integers are singletons
● Everything is an object
● Integers are used all over the place
● Small integers are single...
Small integers are singletons
Beginners confuse is with ==
Identity is not the same as equality
Use is only for objects th...
Some strings are interned
Interning: reusing a singleton copy on demand
● All identifiers are interned
● Many string liter...
When to intern too
Python code can use the intern() function
Use this together with is identity testing
When:
● Large numb...
Peephole optimisations
Peephole optimizer is part of the compiler
● Expressions are simplified
● Some mutable objects are ...
Python will precalculate expressions:
● Numeric calculationsten_days = 10 * 24
* 60 * 60
● Sequencesdefault_retval = (None...
Mutables replaced by immutables
Membership tests against a literal:
if foo in {'ham', 'spam', 'eggs'}
The set() becomes a ...
Introspection and Dissasembly
Demo of object attributes and the dis module
Nächste SlideShare
Wird geladen in …5
×

Python Internals Optimization Choices Made - Codementors Office Hours with Stack Overflow #1 Answerer Martijn Pieters

18.549 Aufrufe

Veröffentlicht am

Codementor (https://www.codementor.io) Python expert mentor Martijn Pieters is a legend on Stack Overflow. A Stack Overflow all-star, he’s amassed a reputation of over 250,000 – ranking him at #2 so far this year and among the top 40 answerers of all time – over the course of responding to more than 10,000 questions.

The CPython developers have made specific optimisations in the interpreter that may affect how your Python code runs. We’ll explore these choices and what they mean for your Python code.

Martijn can be reached at https://www.codementor.io/mjpieters

Veröffentlicht in: Technologie
  • Als Erste(r) kommentieren

Python Internals Optimization Choices Made - Codementors Office Hours with Stack Overflow #1 Answerer Martijn Pieters

  1. 1. How Python internals affect your code Martijn Pieters
  2. 2. 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.
  3. 3. 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
  4. 4. 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.
  5. 5. 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
  6. 6. 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
  7. 7. Peephole optimisations Peephole optimizer is part of the compiler ● Expressions are simplified ● Some mutable objects are replaced with immutables
  8. 8. Python will precalculate expressions: ● Numeric calculationsten_days = 10 * 24 * 60 * 60 ● Sequencesdefault_retval = (None,) * 5 taunt = 'neener' * 3 Expressions are simplified
  9. 9. Mutables replaced by immutables Membership tests against a literal: if foo in {'ham', 'spam', 'eggs'} The set() becomes a frozenset()
  10. 10. Introspection and Dissasembly Demo of object attributes and the dis module

×