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

Arc & Codementor
Arc & CodementorArc & Codementor
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,) *
5
taunt = '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
1 of 10

Recommended

An Introduction to ANTLR by
An Introduction to ANTLRAn Introduction to ANTLR
An Introduction to ANTLRMorteza Zakeri
2.1K views32 slides
PART 1 - Python Tutorial | Variables and Data Types in Python by
PART 1 - Python Tutorial | Variables and Data Types in PythonPART 1 - Python Tutorial | Variables and Data Types in Python
PART 1 - Python Tutorial | Variables and Data Types in PythonShivam Mitra
364 views64 slides
ANTLR4 in depth by
ANTLR4 in depthANTLR4 in depth
ANTLR4 in depthВладимир Кожаев
2.8K views19 slides
Python for All by
Python for All Python for All
Python for All Pragya Goyal
1.1K views17 slides
Programming with Python: Week 1 by
Programming with Python: Week 1Programming with Python: Week 1
Programming with Python: Week 1Ahmet Bulut
442 views39 slides
Python indroduction by
Python indroductionPython indroduction
Python indroductionFEG
96 views35 slides

More Related Content

What's hot

Raspberry using Python Session 1 by
Raspberry using Python Session 1Raspberry using Python Session 1
Raspberry using Python Session 1Mohamed Abd Ela'al
768 views54 slides
Perl::Lint - Yet Another Perl Source Code Linter by
Perl::Lint - Yet Another Perl Source Code LinterPerl::Lint - Yet Another Perl Source Code Linter
Perl::Lint - Yet Another Perl Source Code Lintermoznion
3.3K views154 slides
Unit1 pps by
Unit1 ppsUnit1 pps
Unit1 ppsdeeparengade31
106 views18 slides
PART 9 - Python Tutorial | While Loop In Python With Examples by
PART 9 - Python Tutorial | While Loop In Python With ExamplesPART 9 - Python Tutorial | While Loop In Python With Examples
PART 9 - Python Tutorial | While Loop In Python With ExamplesShivam Mitra
114 views17 slides
Static analysis for perl by
Static analysis for perlStatic analysis for perl
Static analysis for perlmoznion
3.2K views92 slides
Emnlp読み会@2017 02-15 by
Emnlp読み会@2017 02-15Emnlp読み会@2017 02-15
Emnlp読み会@2017 02-15sekizawayuuki
269 views16 slides

What's hot(20)

Perl::Lint - Yet Another Perl Source Code Linter by moznion
Perl::Lint - Yet Another Perl Source Code LinterPerl::Lint - Yet Another Perl Source Code Linter
Perl::Lint - Yet Another Perl Source Code Linter
moznion3.3K views
PART 9 - Python Tutorial | While Loop In Python With Examples by Shivam Mitra
PART 9 - Python Tutorial | While Loop In Python With ExamplesPART 9 - Python Tutorial | While Loop In Python With Examples
PART 9 - Python Tutorial | While Loop In Python With Examples
Shivam Mitra114 views
Static analysis for perl by moznion
Static analysis for perlStatic analysis for perl
Static analysis for perl
moznion3.2K views
Emnlp読み会@2017 02-15 by sekizawayuuki
Emnlp読み会@2017 02-15Emnlp読み会@2017 02-15
Emnlp読み会@2017 02-15
sekizawayuuki269 views
Kotlin & Arrow the functional way by Thoughtworks
Kotlin & Arrow the functional wayKotlin & Arrow the functional way
Kotlin & Arrow the functional way
Thoughtworks826 views
Python presentation by Monu Sharma by Mayank Sharma
Python presentation by Monu SharmaPython presentation by Monu Sharma
Python presentation by Monu Sharma
Mayank Sharma901 views
The role of the parser and Error recovery strategies ppt in compiler design by Sadia Akter
The role of the parser and Error recovery strategies ppt in compiler designThe role of the parser and Error recovery strategies ppt in compiler design
The role of the parser and Error recovery strategies ppt in compiler design
Sadia Akter6.4K views
Pharo: A Reflective System by Marcus Denker
Pharo: A Reflective SystemPharo: A Reflective System
Pharo: A Reflective System
Marcus Denker4.5K views
Part 2 - Python Tutorial | Introduction to Lists by Shivam Mitra
Part 2 - Python Tutorial | Introduction to ListsPart 2 - Python Tutorial | Introduction to Lists
Part 2 - Python Tutorial | Introduction to Lists
Shivam Mitra186 views
About Tokens and Lexemes by Ben Scholzen
About Tokens and LexemesAbout Tokens and Lexemes
About Tokens and Lexemes
Ben Scholzen10.2K views
C# 4.0 and .NET 4.0 by Buu Nguyen
C# 4.0 and .NET 4.0C# 4.0 and .NET 4.0
C# 4.0 and .NET 4.0
Buu Nguyen1.5K views
Getting started with scala cats by Knoldus Inc.
Getting started with scala catsGetting started with scala cats
Getting started with scala cats
Knoldus Inc.74 views
PART 8 - Python Tutorial | User Input In Python With Examples by Shivam Mitra
PART 8 - Python Tutorial | User Input In Python With ExamplesPART 8 - Python Tutorial | User Input In Python With Examples
PART 8 - Python Tutorial | User Input In Python With Examples
Shivam Mitra85 views

Viewers also liked

Advanced templates by
Advanced templatesAdvanced templates
Advanced templatesSencha
3.5K views23 slides
Programación matématica (optimización) con Python - Ingeniería Química - PyConES by
Programación matématica (optimización) con Python - Ingeniería Química - PyConESProgramación matématica (optimización) con Python - Ingeniería Química - PyConES
Programación matématica (optimización) con Python - Ingeniería Química - PyConESCAChemE
6.8K views39 slides
Migrating to Puppet 4.0 by
Migrating to Puppet 4.0Migrating to Puppet 4.0
Migrating to Puppet 4.0Puppet
1.6K views65 slides
Can you upgrade to Puppet 4.x? (Beginner) Can you upgrade to Puppet 4.x? (Beg... by
Can you upgrade to Puppet 4.x? (Beginner) Can you upgrade to Puppet 4.x? (Beg...Can you upgrade to Puppet 4.x? (Beginner) Can you upgrade to Puppet 4.x? (Beg...
Can you upgrade to Puppet 4.x? (Beginner) Can you upgrade to Puppet 4.x? (Beg...Puppet
5.8K views41 slides
Optimizacion con Python (Pyomo vs GAMS vs AMPL) by
Optimizacion con Python (Pyomo vs GAMS vs AMPL)Optimizacion con Python (Pyomo vs GAMS vs AMPL)
Optimizacion con Python (Pyomo vs GAMS vs AMPL)CAChemE
16.9K views44 slides
Python Workshop by
Python  Workshop Python  Workshop
Python Workshop Assem CHELLI
810 views83 slides

Viewers also liked(9)

Advanced templates by Sencha
Advanced templatesAdvanced templates
Advanced templates
Sencha3.5K views
Programación matématica (optimización) con Python - Ingeniería Química - PyConES by CAChemE
Programación matématica (optimización) con Python - Ingeniería Química - PyConESProgramación matématica (optimización) con Python - Ingeniería Química - PyConES
Programación matématica (optimización) con Python - Ingeniería Química - PyConES
CAChemE6.8K views
Migrating to Puppet 4.0 by Puppet
Migrating to Puppet 4.0Migrating to Puppet 4.0
Migrating to Puppet 4.0
Puppet1.6K views
Can you upgrade to Puppet 4.x? (Beginner) Can you upgrade to Puppet 4.x? (Beg... by Puppet
Can you upgrade to Puppet 4.x? (Beginner) Can you upgrade to Puppet 4.x? (Beg...Can you upgrade to Puppet 4.x? (Beginner) Can you upgrade to Puppet 4.x? (Beg...
Can you upgrade to Puppet 4.x? (Beginner) Can you upgrade to Puppet 4.x? (Beg...
Puppet5.8K views
Optimizacion con Python (Pyomo vs GAMS vs AMPL) by CAChemE
Optimizacion con Python (Pyomo vs GAMS vs AMPL)Optimizacion con Python (Pyomo vs GAMS vs AMPL)
Optimizacion con Python (Pyomo vs GAMS vs AMPL)
CAChemE16.9K views
Faster Python Programs Through Optimization by Dr.-Ing Mike Muller by PyData
Faster Python Programs Through Optimization by Dr.-Ing Mike MullerFaster Python Programs Through Optimization by Dr.-Ing Mike Muller
Faster Python Programs Through Optimization by Dr.-Ing Mike Muller
PyData5K views
25 php interview questions – codementor by Arc & Codementor
25 php interview questions – codementor25 php interview questions – codementor
25 php interview questions – codementor
Arc & Codementor173.1K views
20. Object-Oriented Programming Fundamental Principles by Intro C# Book
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles
Intro C# Book473.5K views

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

Python Programming 1.pptx by
Python Programming 1.pptxPython Programming 1.pptx
Python Programming 1.pptxFrancis Densil Raj
27 views258 slides
17575602.ppt by
17575602.ppt17575602.ppt
17575602.pptTejaValmiki
2 views24 slides
Introduction to Python Programming by
Introduction to Python Programming Introduction to Python Programming
Introduction to Python Programming Md. Shafiuzzaman Hira
121 views18 slides
Python for kids - 1.pptx by
Python for kids - 1.pptxPython for kids - 1.pptx
Python for kids - 1.pptxsanyam1427
21 views31 slides
Python Programming.pdf by
Python Programming.pdfPython Programming.pdf
Python Programming.pdfssuser9a6ca1
2 views13 slides
Python - Introduction by
Python - IntroductionPython - Introduction
Python - Introductionstn_tkiller
2.4K views69 slides

Similar to Python Internals Optimization Choices Made - Codementors Office Hours with Stack Overflow #1 Answerer Martijn Pieters(20)

Python for kids - 1.pptx by sanyam1427
Python for kids - 1.pptxPython for kids - 1.pptx
Python for kids - 1.pptx
sanyam142721 views
Python - Introduction by stn_tkiller
Python - IntroductionPython - Introduction
Python - Introduction
stn_tkiller2.4K views
Tutorial on-python-programming by Chetan Giridhar
Tutorial on-python-programmingTutorial on-python-programming
Tutorial on-python-programming
Chetan Giridhar1.4K views
web programming UNIT VIII python by Bhavsingh Maloth by Bhavsingh Maloth
web programming UNIT VIII python by Bhavsingh Malothweb programming UNIT VIII python by Bhavsingh Maloth
web programming UNIT VIII python by Bhavsingh Maloth
Bhavsingh Maloth863 views
Python intro by Piyush rai
Python introPython intro
Python intro
Piyush rai509 views

More from Arc & Codementor

Remote Career Summit 2020 - the State of Remote Jobs - Weiting Liu of Arc by
Remote Career Summit 2020 - the State of Remote Jobs - Weiting Liu of ArcRemote Career Summit 2020 - the State of Remote Jobs - Weiting Liu of Arc
Remote Career Summit 2020 - the State of Remote Jobs - Weiting Liu of ArcArc & Codementor
364 views31 slides
Introduction to Python for Data Science by
Introduction to Python for Data ScienceIntroduction to Python for Data Science
Introduction to Python for Data ScienceArc & Codementor
4.6K views25 slides
20 iOS developer interview questions by
20 iOS developer interview questions20 iOS developer interview questions
20 iOS developer interview questionsArc & Codementor
2.3K views23 slides
29 Essential AngularJS Interview Questions by
29 Essential AngularJS Interview Questions29 Essential AngularJS Interview Questions
29 Essential AngularJS Interview QuestionsArc & Codementor
487.6K views31 slides
37 Java Interview Questions by
37 Java Interview Questions37 Java Interview Questions
37 Java Interview QuestionsArc & Codementor
20.8K views39 slides
21 Essential JavaScript Interview Questions by
21 Essential JavaScript Interview Questions21 Essential JavaScript Interview Questions
21 Essential JavaScript Interview QuestionsArc & Codementor
653.5K views23 slides

More from Arc & Codementor(13)

Remote Career Summit 2020 - the State of Remote Jobs - Weiting Liu of Arc by Arc & Codementor
Remote Career Summit 2020 - the State of Remote Jobs - Weiting Liu of ArcRemote Career Summit 2020 - the State of Remote Jobs - Weiting Liu of Arc
Remote Career Summit 2020 - the State of Remote Jobs - Weiting Liu of Arc
Arc & Codementor364 views
Introduction to Python for Data Science by Arc & Codementor
Introduction to Python for Data ScienceIntroduction to Python for Data Science
Introduction to Python for Data Science
Arc & Codementor4.6K views
20 iOS developer interview questions by Arc & Codementor
20 iOS developer interview questions20 iOS developer interview questions
20 iOS developer interview questions
Arc & Codementor2.3K views
29 Essential AngularJS Interview Questions by Arc & Codementor
29 Essential AngularJS Interview Questions29 Essential AngularJS Interview Questions
29 Essential AngularJS Interview Questions
Arc & Codementor487.6K views
21 Essential JavaScript Interview Questions by Arc & Codementor
21 Essential JavaScript Interview Questions21 Essential JavaScript Interview Questions
21 Essential JavaScript Interview Questions
Arc & Codementor653.5K views
Top 10 Programming Languages in 2015 by Arc & Codementor
Top 10 Programming Languages in 2015Top 10 Programming Languages in 2015
Top 10 Programming Languages in 2015
Arc & Codementor589 views
Angular meteor for angular devs by Arc & Codementor
Angular meteor for angular devsAngular meteor for angular devs
Angular meteor for angular devs
Arc & Codementor18.8K views
Introduction to Tmux - Codementor Tmux Office Hours Part 1 by Arc & Codementor
Introduction to Tmux - Codementor Tmux Office Hours Part 1Introduction to Tmux - Codementor Tmux Office Hours Part 1
Introduction to Tmux - Codementor Tmux Office Hours Part 1
Arc & Codementor42K views
Codementor Office Hours with Eric Chiang: Stdin, Stdout: pup, Go, and life at... by Arc & Codementor
Codementor Office Hours with Eric Chiang: Stdin, Stdout: pup, Go, and life at...Codementor Office Hours with Eric Chiang: Stdin, Stdout: pup, Go, and life at...
Codementor Office Hours with Eric Chiang: Stdin, Stdout: pup, Go, and life at...
Arc & Codementor3.4K views
Building Modern and Secure PHP Applications – Codementor Office Hours with Be... by Arc & Codementor
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...Building Modern and Secure PHP Applications – Codementor Office Hours with Be...
Building Modern and Secure PHP Applications – Codementor Office Hours with Be...
Arc & Codementor11.2K views

Recently uploaded

Uni Systems for Power Platform.pptx by
Uni Systems for Power Platform.pptxUni Systems for Power Platform.pptx
Uni Systems for Power Platform.pptxUni Systems S.M.S.A.
56 views21 slides
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N... by
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...James Anderson
92 views32 slides
Democratising digital commerce in India-Report by
Democratising digital commerce in India-ReportDemocratising digital commerce in India-Report
Democratising digital commerce in India-ReportKapil Khandelwal (KK)
18 views161 slides
HTTP headers that make your website go faster - devs.gent November 2023 by
HTTP headers that make your website go faster - devs.gent November 2023HTTP headers that make your website go faster - devs.gent November 2023
HTTP headers that make your website go faster - devs.gent November 2023Thijs Feryn
22 views151 slides
20231123_Camunda Meetup Vienna.pdf by
20231123_Camunda Meetup Vienna.pdf20231123_Camunda Meetup Vienna.pdf
20231123_Camunda Meetup Vienna.pdfPhactum Softwareentwicklung GmbH
41 views73 slides
6g - REPORT.pdf by
6g - REPORT.pdf6g - REPORT.pdf
6g - REPORT.pdfLiveplex
10 views23 slides

Recently uploaded(20)

GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N... by James Anderson
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
James Anderson92 views
HTTP headers that make your website go faster - devs.gent November 2023 by Thijs Feryn
HTTP headers that make your website go faster - devs.gent November 2023HTTP headers that make your website go faster - devs.gent November 2023
HTTP headers that make your website go faster - devs.gent November 2023
Thijs Feryn22 views
6g - REPORT.pdf by Liveplex
6g - REPORT.pdf6g - REPORT.pdf
6g - REPORT.pdf
Liveplex10 views
STKI Israeli Market Study 2023 corrected forecast 2023_24 v3.pdf by Dr. Jimmy Schwarzkopf
STKI Israeli Market Study 2023   corrected forecast 2023_24 v3.pdfSTKI Israeli Market Study 2023   corrected forecast 2023_24 v3.pdf
STKI Israeli Market Study 2023 corrected forecast 2023_24 v3.pdf
Future of AR - Facebook Presentation by ssuserb54b561
Future of AR - Facebook PresentationFuture of AR - Facebook Presentation
Future of AR - Facebook Presentation
ssuserb54b56115 views
STPI OctaNE CoE Brochure.pdf by madhurjyapb
STPI OctaNE CoE Brochure.pdfSTPI OctaNE CoE Brochure.pdf
STPI OctaNE CoE Brochure.pdf
madhurjyapb14 views
SAP Automation Using Bar Code and FIORI.pdf by Virendra Rai, PMP
SAP Automation Using Bar Code and FIORI.pdfSAP Automation Using Bar Code and FIORI.pdf
SAP Automation Using Bar Code and FIORI.pdf
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas... by Bernd Ruecker
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
Bernd Ruecker40 views
ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ... by Jasper Oosterveld
ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ...ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ...
ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ...
Special_edition_innovator_2023.pdf by WillDavies22
Special_edition_innovator_2023.pdfSpecial_edition_innovator_2023.pdf
Special_edition_innovator_2023.pdf
WillDavies2218 views
TouchLog: Finger Micro Gesture Recognition Using Photo-Reflective Sensors by sugiuralab
TouchLog: Finger Micro Gesture Recognition  Using Photo-Reflective SensorsTouchLog: Finger Micro Gesture Recognition  Using Photo-Reflective Sensors
TouchLog: Finger Micro Gesture Recognition Using Photo-Reflective Sensors
sugiuralab21 views
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院 by IttrainingIttraining
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院

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

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

Editor's Notes

  1. Everything in Python is an object. This includes integers. And integers are used all over the place; ask for the length of something? The result is an integer object. Want to index into a list? You have to create an integer object for that. Etc. To save on creating and destroying too many objects, the most commonly used integer values are cached; you always get the *same, single copy* of such integers. That’s fine, because these objects are immutable, sharing a single copy carries no risk of corruption. The values -5 through to 256 are all singletons.
  2. Why does this matter? Beginners may get confused between `is` and `==` , between identity testing and equality testing. The former tests if two object references are pointing to the same object, while the latter tests if two objects contain the same value. Since these small integers are all singletons, `is` *always works for these*; if you have a value 42 in one place and another 42 in another, they are always the same object. But for larger integers this fails and confusion ensues. Demo: foo = 6 * 8 bar = 40 + 2 foo == bar foo is bar
  3. Some strings are cached too; they are *interned*. Interning means that the interpreter will explicitly reuse a singleton version of strings. Small integers are reused by the constructor, so *always*. Interned strings are only reused explicitly, the interpreter makes a decision to intern a string when creating one. In Python, all identifiers (names in your program, including attributes on objects) are interned. Creating a class? Then your class name, all attributes including the method names and all arguments and local names in the functions are all interned. When you create a string literal, so a string value in quotes, and the value *looks enough* like an identifier, then it is interned too. Why was this done? Namespaces in Python are dictionaries. A lookup in a dictionary is fast thanks to hashing, but always requires an equality test on the key too, because hashing into the dictionary table is not unique. Python code does **loads** of namespace lookups, all the time. Python first tests if the C pointers are the same, an identity test, because that is so much faster than a string comparison, character by character.
  4. If your program has to handle a *lot* of text lookups (in dictionaries, for example), it could be advantageous to use interning too. You can use the built-in function `intern()` to produce singletons; apply it judiciously to your dictionary keys and anywhere you want to test for those keys.
  5. The peephole optimizer is part of the Python compiler. It applies a few tricks to your code. The Python compiler stores not just code, but also constants; integers, strings, tuples, anything that is immutable and defined with your code is stored as a constant for quick and easy access. To aid in this: Expressions are simplified Some mutable objects are replaced with immutables (Next slides elaborate)
  6. Mutable literals in membership tests are replaced with an immutable variant list -> tuple set -> frozenset Membership testing in sets is faster than using a tuple, make use of it!
  7. Demo showing constants in introspection