SlideShare a Scribd company logo
Python Peculiarities
Noam Tenne
$WHOAMI
Hacking around for the past ~15 years
@NoamTenne
http://blog.10ne.org
Best company
ever!
$WHOAMI
Hacking around for the past ~15 years
@NoamTenne
http://blog.10ne.org
Best company
ever!
We’re hiring!
$WHOAMI
github.com/noamt
“Cafe Bots” podcast together with Yoav Luft and Tom Kaminski
How this works
How this works
• 1 entertaining dude at your service
How this works
• 1 entertaining dude at your service
•We review puzzles
How this works
• 1 entertaining dude at your service
•We review puzzles
•You vote for the correct answer
First Rule!
The Princess Py
name = "Inigo Montoya"
victim = "my father"
consequence = "die"
from os import *
print("Hello, my name is %s. You killed %s, prepare to %s" %
(name, victim, consequence))
https://codegolf.stackexchange.com/a/11467
The Princess Py
name = "Inigo Montoya"
victim = "my father"
consequence = "die"
from os import *
print("Hello, my name is %s. You killed %s, prepare to %s" %
(name, victim, consequence))
1. Hello, my name is posix. You killed my father, prepare to die

2. Hello, my name is Inigo Montoya. You killed my father, prepare to die

3. Hello, my name is <built-in function name>. You killed my father,
prepare to die

4. SyntaxError: invalid syntax
https://codegolf.stackexchange.com/a/11467
The Princess Py
name = "Inigo Montoya"
victim = "my father"
consequence = "die"
from os import *
print("Hello, my name is %s. You killed %s, prepare to %s" %
(name, victim, consequence))
1. Hello, my name is posix. You killed my father, prepare to die

2. Hello, my name is Inigo Montoya. You killed my father, prepare to die

3. Hello, my name is <built-in function name>. You killed my father,
prepare to die

4. SyntaxError: invalid syntax
https://codegolf.stackexchange.com/a/11467
Python Peculiarities
Python Peculiarities
What’s going on?
...
from os import *
...
This guy
What’s going on?
...
from os import *
...
This guy
Imports
this guy
What’s going on?
...
from os import *
...
name = "Inigo Montoya"
This guy
Imports
this guy
And
overrides
this guy
How do we fix it?
Don’t use import * !
Floating Votes
>>> number_of_votes = 5
>>> number_of_votes.__str__()
'5'
>>> 5.__str__()
http://blog.brush.co.nz/2008/01/ten-python-quirkies/
Floating Votes
>>> number_of_votes = 5
>>> number_of_votes.__str__()
'5'
>>> 5.__str__()
1. 5

2. 5.0

3. <method-wrapper '__str__' of int object at 0x1054e5b50>

4. SyntaxError: invalid syntax
http://blog.brush.co.nz/2008/01/ten-python-quirkies/
Floating Votes
>>> number_of_votes = 5
>>> number_of_votes.__str__()
'5'
>>> 5.__str__()
1. 5

2. 5.0

3. <method-wrapper '__str__' of int object at 0x1054e5b50>

4. SyntaxError: invalid syntax
http://blog.brush.co.nz/2008/01/ten-python-quirkies/
Python Peculiarities
Python Peculiarities
What’s going on?
>>> number_of_votes = 5
>>> number_of_votes.__str__()
'5'
>>> 5.__str__()
What’s going on?
>>> number_of_votes = 5
>>> number_of_votes.__str__()
'5'
>>> 5.__str__()
What is this?
How do we fix it?
Help the parser!

Use parentheses!
>>> (5).__str__()
How do we fix it?
Prefer core methods over attributes
>>> str(5)
It’s All Your Default
def func(A=[]): A.append(42); return A
print(func())
print(func())
http://blog.brush.co.nz/2008/01/ten-python-quirkies/
It’s All Your Default
def func(A=[]): A.append(42); return A
print(func())
print(func())
1. []n[]

2. [42]n[42]
3. [42]n[84]

4. [42]n[42,42]
http://blog.brush.co.nz/2008/01/ten-python-quirkies/
It’s All Your Default
def func(A=[]): A.append(42); return A
print(func())
print(func())
1. []n[]

2. [42]n[42]
3. [42]n[84]

4. [42]n[42,42]
http://blog.brush.co.nz/2008/01/ten-python-quirkies/
Python Peculiarities
Python Peculiarities
What’s going on?
def func(A=[]): A.append(42); return A
What’s going on?
def func(A=[]): A.append(42); return A
Default methods
evaluated once at
compile time
Uncle Barry
from __future__ import barry_as_FLUFL
print(42 != 42)
https://stackoverflow.com/q/4007289/198825
Uncle Barry
from __future__ import barry_as_FLUFL
print(42 != 42)
1. SyntaxError: invalid syntax

2. True
3. False

4. 84
https://stackoverflow.com/q/4007289/198825
Uncle Barry
from __future__ import barry_as_FLUFL
print(42 != 42)
1. SyntaxError: invalid syntax

2. True
3. False

4. 84
https://stackoverflow.com/q/4007289/198825
Python Peculiarities
Python Peculiarities
What’s going on?
from __future__ import barry_as_FLUFL
PEP 401 -- BDFL Retirement
What’s going on?
from __future__ import barry_as_FLUFL
Replaces
!=
with
<>PEP 401 -- BDFL Retirement
How do we fix it?
print(42 != 42)
print(42 <> 42)
Change
To
How do we fix it?
-or-
Don’t use silly modules!
print(42 != 42)
print(42 <> 42)
Change
To
The Future!
from __future__ import braces
exec("if (input() > 0) {nprint 'it's the future!';n}")
https://codegolf.stackexchange.com/a/11450
The Future!
from __future__ import braces
exec("if (input() > 0) {nprint 'it's the future!';n}")
1. Prints Nothing

2. Prints “it’s the future!”

3. SyntaxError: invalid syntax

4. SyntaxError: not a chance
https://codegolf.stackexchange.com/a/11450
The Future!
from __future__ import braces
exec("if (input() > 0) {nprint 'it's the future!';n}")
1. Prints Nothing

2. Prints “it’s the future!”

3. SyntaxError: invalid syntax

4. SyntaxError: not a chance
https://codegolf.stackexchange.com/a/11450
Python Peculiarities
Python Peculiarities
What’s going on?
from __future__ import braces
An easter egg
Python has many fun easter eggs!
Python has many fun easter eggs!
import __hello__
Python has many fun easter eggs!
import this
import __hello__
Python has many fun easter eggs!
import this
import __hello__
import antigravity
Commaleon
list_a = [1, 3, 3, 7],
list_b = [1, 3, 3, 7]
list_a.extend(list_b)
print(list_a)
http://bit.ly/2r7yZwY
Commaleon
list_a = [1, 3, 3, 7],
list_b = [1, 3, 3, 7]
list_a.extend(list_b)
print(list_a)
1. [[1, 3, 3, 7], 1, 3, 3, 7]

2. [1, 3, 3, 7, 1, 3, 3, 7]

3. [[1, 3, 3, 7], [1, 3, 3, 7]]

4. AttributeError
http://bit.ly/2r7yZwY
Commaleon
list_a = [1, 3, 3, 7],
list_b = [1, 3, 3, 7]
list_a.extend(list_b)
print(list_a)
1. [[1, 3, 3, 7], 1, 3, 3, 7]

2. [1, 3, 3, 7, 1, 3, 3, 7]

3. [[1, 3, 3, 7], [1, 3, 3, 7]]

4. AttributeError
http://bit.ly/2r7yZwY
Python Peculiarities
Python Peculiarities
What’s going on?
list_a = [1, 3, 3, 7],
list_b = [1, 3, 3, 7]
list_a.extend(list_b)
print(list_a)
What’s going on?
list_a = [1, 3, 3, 7],
list_b = [1, 3, 3, 7]
list_a.extend(list_b)
print(list_a)
list_a is now

a tuple
What’s going on?
list_a = [1, 3, 3, 7],
list_b = [1, 3, 3, 7]
list_a.extend(list_b)
print(list_a)
list_a is now

a tuple
Tupples have no
extend attribute
How do we fix it?
Use static analysis rules
Johnny Cache
>>> 1*2
2
>>> _*3
6
>>> print(_*4)
24
>>> print(_*5)
http://bit.ly/2rfOMhe
Johnny Cache
>>> 1*2
2
>>> _*3
6
>>> print(_*4)
24
>>> print(_*5)
1. SyntaxError
2. AttributeError

3. 108

4. 30
http://bit.ly/2rfOMhe
Johnny Cache
>>> 1*2
2
>>> _*3
6
>>> print(_*4)
24
>>> print(_*5)
1. SyntaxError
2. AttributeError

3. 108

4. 30
http://bit.ly/2rfOMhe
Python Peculiarities
Python Peculiarities
What’s going on?
>>> 1*2
2
>>> _*3
6
>>> print(_*4)
24
>>> print(_*5)
What’s going on?
_ accesses last
returned value
>>> 1*2
2
>>> _*3
6
>>> print(_*4)
24
>>> print(_*5)
What’s going on?
_ accesses last
returned value
Return value of
print is None
>>> 1*2
2
>>> _*3
6
>>> print(_*4)
24
>>> print(_*5)
Multiplication Complication
multiplication = []
for i in range(10):
multiplication.append(i * ++i)
for multiplication[i] in multiplication:
print(multiplication[i])
https://codegolf.stackexchange.com/a/11480
Multiplication Complication
multiplication = []
for i in range(10):
multiplication.append(i * ++i)
for multiplication[i] in multiplication:
print(multiplication[i])
1. 0, 1, 4, 9, 16, 25, 36, 49, 64, 81

2. 0, 1, 4, 9, 16, 25, 36, 49, 64, 64

3. SyntaxError

4. 0, 2, 6, 12, 20, 30, 42, 56, 72, 90
https://codegolf.stackexchange.com/a/11480
Multiplication Complication
multiplication = []
for i in range(10):
multiplication.append(i * ++i)
for multiplication[i] in multiplication:
print(multiplication[i])
1. 0, 1, 4, 9, 16, 25, 36, 49, 64, 81

2. 0, 1, 4, 9, 16, 25, 36, 49, 64, 64

3. SyntaxError

4. 0, 2, 6, 12, 20, 30, 42, 56, 72, 90
https://codegolf.stackexchange.com/a/11480
Python Peculiarities
Python Peculiarities
What’s going on?
multiplication = []
for i in range(10):
multiplication.append(i * ++i)
What’s going on?
multiplication = []
for i in range(10):
multiplication.append(i * ++i)
Not a
valid
operation
What’s going on?
multiplication = []
for i in range(10):
multiplication.append(i * ++i)
Not a
valid
operation
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
What’s going on?
multiplication = []
for i in range(10):
multiplication.append(i * ++i)
Not a
valid
operation
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
for multiplication[i] in multiplication:
Assigns each
element to index 9
What’s going on?
multiplication = []
for i in range(10):
multiplication.append(i * ++i)
Not a
valid
operation
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
for multiplication[i] in multiplication:
Assigns each
element to index 9
[0, 1, 4, 9, 16, 25, 36, 49, 64, 0]
What’s going on?
multiplication = []
for i in range(10):
multiplication.append(i * ++i)
Not a
valid
operation
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
for multiplication[i] in multiplication:
Assigns each
element to index 9
[0, 1, 4, 9, 16, 25, 36, 49, 64, 1]
What’s going on?
multiplication = []
for i in range(10):
multiplication.append(i * ++i)
Not a
valid
operation
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
for multiplication[i] in multiplication:
Assigns each
element to index 9
[0, 1, 4, 9, 16, 25, 36, 49, 64, 4]
What’s going on?
multiplication = []
for i in range(10):
multiplication.append(i * ++i)
Not a
valid
operation
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
for multiplication[i] in multiplication:
Assigns each
element to index 9
[0, 1, 4, 9, 16, 25, 36, 49, 64, 64]
What’s going on?
multiplication = []
for i in range(10):
multiplication.append(i * ++i)
Not a
valid
operation
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
for multiplication[i] in multiplication:
Assigns each
element to index 9
Some take-aways
Some take-aways
1. Write readable code
Some take-aways
1. Write readable code
2.Document/Comment your tricks and hacks
Some take-aways
1. Write readable code
2.Document/Comment your tricks and hacks
3.Use static analysis and a good IDE
Some take-aways
1. Write readable code
2.Document/Comment your tricks and hacks
3.Use static analysis and a good IDE
4.RTFM!
Positive Feedback?
@NoamTenne
noam@10ne.org
Negative Feedback?
/dev/null
Queries?
Thanks!

More Related Content

PDF
Python for High School Programmers
Siva Arunachalam
 
PDF
Python Tutorial
Eueung Mulyana
 
PDF
Python fundamentals - basic | WeiYuan
Wei-Yuan Chang
 
PDF
Begin with Python
Narong Intiruk
 
PDF
Python and sysadmin I
Guixing Bai
 
PDF
Matlab and Python: Basic Operations
Wai Nwe Tun
 
PDF
Python于Web 2.0网站的应用 - QCon Beijing 2010
Qiangning Hong
 
Python for High School Programmers
Siva Arunachalam
 
Python Tutorial
Eueung Mulyana
 
Python fundamentals - basic | WeiYuan
Wei-Yuan Chang
 
Begin with Python
Narong Intiruk
 
Python and sysadmin I
Guixing Bai
 
Matlab and Python: Basic Operations
Wai Nwe Tun
 
Python于Web 2.0网站的应用 - QCon Beijing 2010
Qiangning Hong
 

What's hot (20)

PDF
Advanced Python, Part 2
Zaar Hai
 
PDF
Learn 90% of Python in 90 Minutes
Matt Harrison
 
PDF
AmI 2016 - Python basics
Luigi De Russis
 
PPTX
Down the rabbit hole, profiling in Django
Remco Wendt
 
ODP
Programming Under Linux In Python
Marwan Osman
 
PDF
Introducción a Elixir
Svet Ivantchev
 
PPT
java 8 Hands on Workshop
Jeanne Boyarsky
 
PPT
Python легко и просто. Красиво решаем повседневные задачи
Maxim Kulsha
 
PDF
AmI 2015 - Python basics
Luigi De Russis
 
PPTX
Learn python - for beginners - part-2
RajKumar Rampelli
 
PDF
Functions
Marieswaran Ramasamy
 
PDF
Introduction to advanced python
Charles-Axel Dein
 
PPTX
Python 101++: Let's Get Down to Business!
Paige Bailey
 
PPTX
Python Traning presentation
Nimrita Koul
 
PPTX
Introduction to Python and TensorFlow
Bayu Aldi Yansyah
 
PDF
Python Part 1
Mohamed Ramadan
 
PDF
Commit ускоривший python 2.7.11 на 30% и новое в python 3.5
PyNSK
 
PDF
Going Loopy - Adventures in Iteration with Google Go
Eleanor McHugh
 
PDF
The Error of Our Ways
Kevlin Henney
 
PPTX
Learn python in 20 minutes
Sidharth Nadhan
 
Advanced Python, Part 2
Zaar Hai
 
Learn 90% of Python in 90 Minutes
Matt Harrison
 
AmI 2016 - Python basics
Luigi De Russis
 
Down the rabbit hole, profiling in Django
Remco Wendt
 
Programming Under Linux In Python
Marwan Osman
 
Introducción a Elixir
Svet Ivantchev
 
java 8 Hands on Workshop
Jeanne Boyarsky
 
Python легко и просто. Красиво решаем повседневные задачи
Maxim Kulsha
 
AmI 2015 - Python basics
Luigi De Russis
 
Learn python - for beginners - part-2
RajKumar Rampelli
 
Introduction to advanced python
Charles-Axel Dein
 
Python 101++: Let's Get Down to Business!
Paige Bailey
 
Python Traning presentation
Nimrita Koul
 
Introduction to Python and TensorFlow
Bayu Aldi Yansyah
 
Python Part 1
Mohamed Ramadan
 
Commit ускоривший python 2.7.11 на 30% и новое в python 3.5
PyNSK
 
Going Loopy - Adventures in Iteration with Google Go
Eleanor McHugh
 
The Error of Our Ways
Kevlin Henney
 
Learn python in 20 minutes
Sidharth Nadhan
 
Ad

Similar to Python Peculiarities (20)

DOCX
Mcq cpup
tahir_ali786
 
PDF
Advanced Debugging Using Java Bytecodes
Ganesh Samarthyam
 
PPTX
Java SE 17 Study Guide for Certification - Chapter 02
williamrobertherman
 
PDF
[ROOTCON13] Pilot Study on Semi-Automated Patch Diffing by Applying Machine-L...
Asuka Nakajima
 
PDF
Python slide
Kiattisak Anoochitarom
 
PDF
pa-pe-pi-po-pure Python Text Processing
Rodrigo Senra
 
PPTX
16. Java stacks and queues
Intro C# Book
 
PDF
Python
Helio Colombe
 
PDF
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
Arnaud Joly
 
PDF
Technology: A Means to an End with Thibault Imbert
FITC
 
PDF
FITC '14 Toronto - Technology, a means to an end
Thibault Imbert
 
PDF
DSA 103 Object Oriented Programming :: Week 3
Ferdin Joe John Joseph PhD
 
PDF
C++ Searching & Sorting5. Sort the following list using the select.pdf
Rahul04August
 
PDF
C++ L03-Control Structure
Mohammad Shaker
 
PDF
Python Fundamentals - Basic
Wei-Yuan Chang
 
PDF
C++ L04-Array+String
Mohammad Shaker
 
PPT
Rocky Nevin's presentation at eComm 2008
eComm2008
 
PDF
What We Talk About When We Talk About Unit Testing
Kevlin Henney
 
ODP
Introduction to Python3 Programming Language
Tushar Mittal
 
PDF
How to avoid Go gotchas - Ivan Daniluk - Codemotion Milan 2016
Codemotion
 
Mcq cpup
tahir_ali786
 
Advanced Debugging Using Java Bytecodes
Ganesh Samarthyam
 
Java SE 17 Study Guide for Certification - Chapter 02
williamrobertherman
 
[ROOTCON13] Pilot Study on Semi-Automated Patch Diffing by Applying Machine-L...
Asuka Nakajima
 
pa-pe-pi-po-pure Python Text Processing
Rodrigo Senra
 
16. Java stacks and queues
Intro C# Book
 
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
Arnaud Joly
 
Technology: A Means to an End with Thibault Imbert
FITC
 
FITC '14 Toronto - Technology, a means to an end
Thibault Imbert
 
DSA 103 Object Oriented Programming :: Week 3
Ferdin Joe John Joseph PhD
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
Rahul04August
 
C++ L03-Control Structure
Mohammad Shaker
 
Python Fundamentals - Basic
Wei-Yuan Chang
 
C++ L04-Array+String
Mohammad Shaker
 
Rocky Nevin's presentation at eComm 2008
eComm2008
 
What We Talk About When We Talk About Unit Testing
Kevlin Henney
 
Introduction to Python3 Programming Language
Tushar Mittal
 
How to avoid Go gotchas - Ivan Daniluk - Codemotion Milan 2016
Codemotion
 
Ad

More from noamt (7)

PDF
We Need A Better Testing Framework (2019)
noamt
 
PDF
Go Modules
noamt
 
PDF
Functional Groovy
noamt
 
PDF
We Need A Better Testing Framework
noamt
 
PDF
HTTPBuilder NG: Back From The Dead
noamt
 
PDF
Groovy Powered Clean Code
noamt
 
PDF
Searching for the grail
noamt
 
We Need A Better Testing Framework (2019)
noamt
 
Go Modules
noamt
 
Functional Groovy
noamt
 
We Need A Better Testing Framework
noamt
 
HTTPBuilder NG: Back From The Dead
noamt
 
Groovy Powered Clean Code
noamt
 
Searching for the grail
noamt
 

Recently uploaded (20)

PDF
Salesforce Implementation Services Provider.pdf
VALiNTRY360
 
PPT
Activate_Methodology_Summary presentatio
annapureddyn
 
PPTX
Contractor Management Platform and Software Solution for Compliance
SHEQ Network Limited
 
PDF
New Download MiniTool Partition Wizard Crack Latest Version 2025
imang66g
 
PPTX
classification of computer and basic part of digital computer
ravisinghrajpurohit3
 
PDF
Adobe Illustrator Crack Full Download (Latest Version 2025) Pre-Activated
imang66g
 
PDF
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 
PDF
Using licensed Data Loss Prevention (DLP) as a strategic proactive data secur...
Q-Advise
 
PDF
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
PPTX
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
PDF
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
PDF
Generating Union types w/ Static Analysis
K. Matthew Dupree
 
PDF
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
PDF
WatchTraderHub - Watch Dealer software with inventory management and multi-ch...
WatchDealer Pavel
 
PDF
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
PPTX
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
PDF
Exploring AI Agents in Process Industries
amoreira6
 
PDF
vAdobe Premiere Pro 2025 (v25.2.3.004) Crack Pre-Activated Latest
imang66g
 
PDF
Download iTop VPN Free 6.1.0.5882 Crack Full Activated Pre Latest 2025
imang66g
 
DOCX
Can You Build Dashboards Using Open Source Visualization Tool.docx
Varsha Nayak
 
Salesforce Implementation Services Provider.pdf
VALiNTRY360
 
Activate_Methodology_Summary presentatio
annapureddyn
 
Contractor Management Platform and Software Solution for Compliance
SHEQ Network Limited
 
New Download MiniTool Partition Wizard Crack Latest Version 2025
imang66g
 
classification of computer and basic part of digital computer
ravisinghrajpurohit3
 
Adobe Illustrator Crack Full Download (Latest Version 2025) Pre-Activated
imang66g
 
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 
Using licensed Data Loss Prevention (DLP) as a strategic proactive data secur...
Q-Advise
 
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
Generating Union types w/ Static Analysis
K. Matthew Dupree
 
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
WatchTraderHub - Watch Dealer software with inventory management and multi-ch...
WatchDealer Pavel
 
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
Exploring AI Agents in Process Industries
amoreira6
 
vAdobe Premiere Pro 2025 (v25.2.3.004) Crack Pre-Activated Latest
imang66g
 
Download iTop VPN Free 6.1.0.5882 Crack Full Activated Pre Latest 2025
imang66g
 
Can You Build Dashboards Using Open Source Visualization Tool.docx
Varsha Nayak
 

Python Peculiarities

  • 2. $WHOAMI Hacking around for the past ~15 years @NoamTenne http://blog.10ne.org Best company ever!
  • 3. $WHOAMI Hacking around for the past ~15 years @NoamTenne http://blog.10ne.org Best company ever! We’re hiring!
  • 4. $WHOAMI github.com/noamt “Cafe Bots” podcast together with Yoav Luft and Tom Kaminski
  • 6. How this works • 1 entertaining dude at your service
  • 7. How this works • 1 entertaining dude at your service •We review puzzles
  • 8. How this works • 1 entertaining dude at your service •We review puzzles •You vote for the correct answer
  • 10. The Princess Py name = "Inigo Montoya" victim = "my father" consequence = "die" from os import * print("Hello, my name is %s. You killed %s, prepare to %s" % (name, victim, consequence)) https://codegolf.stackexchange.com/a/11467
  • 11. The Princess Py name = "Inigo Montoya" victim = "my father" consequence = "die" from os import * print("Hello, my name is %s. You killed %s, prepare to %s" % (name, victim, consequence)) 1. Hello, my name is posix. You killed my father, prepare to die
 2. Hello, my name is Inigo Montoya. You killed my father, prepare to die
 3. Hello, my name is <built-in function name>. You killed my father, prepare to die
 4. SyntaxError: invalid syntax https://codegolf.stackexchange.com/a/11467
  • 12. The Princess Py name = "Inigo Montoya" victim = "my father" consequence = "die" from os import * print("Hello, my name is %s. You killed %s, prepare to %s" % (name, victim, consequence)) 1. Hello, my name is posix. You killed my father, prepare to die
 2. Hello, my name is Inigo Montoya. You killed my father, prepare to die
 3. Hello, my name is <built-in function name>. You killed my father, prepare to die
 4. SyntaxError: invalid syntax https://codegolf.stackexchange.com/a/11467
  • 15. What’s going on? ... from os import * ... This guy
  • 16. What’s going on? ... from os import * ... This guy Imports this guy
  • 17. What’s going on? ... from os import * ... name = "Inigo Montoya" This guy Imports this guy And overrides this guy
  • 18. How do we fix it? Don’t use import * !
  • 19. Floating Votes >>> number_of_votes = 5 >>> number_of_votes.__str__() '5' >>> 5.__str__() http://blog.brush.co.nz/2008/01/ten-python-quirkies/
  • 20. Floating Votes >>> number_of_votes = 5 >>> number_of_votes.__str__() '5' >>> 5.__str__() 1. 5
 2. 5.0
 3. <method-wrapper '__str__' of int object at 0x1054e5b50>
 4. SyntaxError: invalid syntax http://blog.brush.co.nz/2008/01/ten-python-quirkies/
  • 21. Floating Votes >>> number_of_votes = 5 >>> number_of_votes.__str__() '5' >>> 5.__str__() 1. 5
 2. 5.0
 3. <method-wrapper '__str__' of int object at 0x1054e5b50>
 4. SyntaxError: invalid syntax http://blog.brush.co.nz/2008/01/ten-python-quirkies/
  • 24. What’s going on? >>> number_of_votes = 5 >>> number_of_votes.__str__() '5' >>> 5.__str__()
  • 25. What’s going on? >>> number_of_votes = 5 >>> number_of_votes.__str__() '5' >>> 5.__str__() What is this?
  • 26. How do we fix it? Help the parser!
 Use parentheses! >>> (5).__str__()
  • 27. How do we fix it? Prefer core methods over attributes >>> str(5)
  • 28. It’s All Your Default def func(A=[]): A.append(42); return A print(func()) print(func()) http://blog.brush.co.nz/2008/01/ten-python-quirkies/
  • 29. It’s All Your Default def func(A=[]): A.append(42); return A print(func()) print(func()) 1. []n[]
 2. [42]n[42] 3. [42]n[84]
 4. [42]n[42,42] http://blog.brush.co.nz/2008/01/ten-python-quirkies/
  • 30. It’s All Your Default def func(A=[]): A.append(42); return A print(func()) print(func()) 1. []n[]
 2. [42]n[42] 3. [42]n[84]
 4. [42]n[42,42] http://blog.brush.co.nz/2008/01/ten-python-quirkies/
  • 33. What’s going on? def func(A=[]): A.append(42); return A
  • 34. What’s going on? def func(A=[]): A.append(42); return A Default methods evaluated once at compile time
  • 35. Uncle Barry from __future__ import barry_as_FLUFL print(42 != 42) https://stackoverflow.com/q/4007289/198825
  • 36. Uncle Barry from __future__ import barry_as_FLUFL print(42 != 42) 1. SyntaxError: invalid syntax
 2. True 3. False
 4. 84 https://stackoverflow.com/q/4007289/198825
  • 37. Uncle Barry from __future__ import barry_as_FLUFL print(42 != 42) 1. SyntaxError: invalid syntax
 2. True 3. False
 4. 84 https://stackoverflow.com/q/4007289/198825
  • 40. What’s going on? from __future__ import barry_as_FLUFL PEP 401 -- BDFL Retirement
  • 41. What’s going on? from __future__ import barry_as_FLUFL Replaces != with <>PEP 401 -- BDFL Retirement
  • 42. How do we fix it? print(42 != 42) print(42 <> 42) Change To
  • 43. How do we fix it? -or- Don’t use silly modules! print(42 != 42) print(42 <> 42) Change To
  • 44. The Future! from __future__ import braces exec("if (input() > 0) {nprint 'it's the future!';n}") https://codegolf.stackexchange.com/a/11450
  • 45. The Future! from __future__ import braces exec("if (input() > 0) {nprint 'it's the future!';n}") 1. Prints Nothing
 2. Prints “it’s the future!”
 3. SyntaxError: invalid syntax
 4. SyntaxError: not a chance https://codegolf.stackexchange.com/a/11450
  • 46. The Future! from __future__ import braces exec("if (input() > 0) {nprint 'it's the future!';n}") 1. Prints Nothing
 2. Prints “it’s the future!”
 3. SyntaxError: invalid syntax
 4. SyntaxError: not a chance https://codegolf.stackexchange.com/a/11450
  • 49. What’s going on? from __future__ import braces An easter egg
  • 50. Python has many fun easter eggs!
  • 51. Python has many fun easter eggs! import __hello__
  • 52. Python has many fun easter eggs! import this import __hello__
  • 53. Python has many fun easter eggs! import this import __hello__ import antigravity
  • 54. Commaleon list_a = [1, 3, 3, 7], list_b = [1, 3, 3, 7] list_a.extend(list_b) print(list_a) http://bit.ly/2r7yZwY
  • 55. Commaleon list_a = [1, 3, 3, 7], list_b = [1, 3, 3, 7] list_a.extend(list_b) print(list_a) 1. [[1, 3, 3, 7], 1, 3, 3, 7]
 2. [1, 3, 3, 7, 1, 3, 3, 7]
 3. [[1, 3, 3, 7], [1, 3, 3, 7]]
 4. AttributeError http://bit.ly/2r7yZwY
  • 56. Commaleon list_a = [1, 3, 3, 7], list_b = [1, 3, 3, 7] list_a.extend(list_b) print(list_a) 1. [[1, 3, 3, 7], 1, 3, 3, 7]
 2. [1, 3, 3, 7, 1, 3, 3, 7]
 3. [[1, 3, 3, 7], [1, 3, 3, 7]]
 4. AttributeError http://bit.ly/2r7yZwY
  • 59. What’s going on? list_a = [1, 3, 3, 7], list_b = [1, 3, 3, 7] list_a.extend(list_b) print(list_a)
  • 60. What’s going on? list_a = [1, 3, 3, 7], list_b = [1, 3, 3, 7] list_a.extend(list_b) print(list_a) list_a is now
 a tuple
  • 61. What’s going on? list_a = [1, 3, 3, 7], list_b = [1, 3, 3, 7] list_a.extend(list_b) print(list_a) list_a is now
 a tuple Tupples have no extend attribute
  • 62. How do we fix it? Use static analysis rules
  • 63. Johnny Cache >>> 1*2 2 >>> _*3 6 >>> print(_*4) 24 >>> print(_*5) http://bit.ly/2rfOMhe
  • 64. Johnny Cache >>> 1*2 2 >>> _*3 6 >>> print(_*4) 24 >>> print(_*5) 1. SyntaxError 2. AttributeError
 3. 108
 4. 30 http://bit.ly/2rfOMhe
  • 65. Johnny Cache >>> 1*2 2 >>> _*3 6 >>> print(_*4) 24 >>> print(_*5) 1. SyntaxError 2. AttributeError
 3. 108
 4. 30 http://bit.ly/2rfOMhe
  • 68. What’s going on? >>> 1*2 2 >>> _*3 6 >>> print(_*4) 24 >>> print(_*5)
  • 69. What’s going on? _ accesses last returned value >>> 1*2 2 >>> _*3 6 >>> print(_*4) 24 >>> print(_*5)
  • 70. What’s going on? _ accesses last returned value Return value of print is None >>> 1*2 2 >>> _*3 6 >>> print(_*4) 24 >>> print(_*5)
  • 71. Multiplication Complication multiplication = [] for i in range(10): multiplication.append(i * ++i) for multiplication[i] in multiplication: print(multiplication[i]) https://codegolf.stackexchange.com/a/11480
  • 72. Multiplication Complication multiplication = [] for i in range(10): multiplication.append(i * ++i) for multiplication[i] in multiplication: print(multiplication[i]) 1. 0, 1, 4, 9, 16, 25, 36, 49, 64, 81
 2. 0, 1, 4, 9, 16, 25, 36, 49, 64, 64
 3. SyntaxError
 4. 0, 2, 6, 12, 20, 30, 42, 56, 72, 90 https://codegolf.stackexchange.com/a/11480
  • 73. Multiplication Complication multiplication = [] for i in range(10): multiplication.append(i * ++i) for multiplication[i] in multiplication: print(multiplication[i]) 1. 0, 1, 4, 9, 16, 25, 36, 49, 64, 81
 2. 0, 1, 4, 9, 16, 25, 36, 49, 64, 64
 3. SyntaxError
 4. 0, 2, 6, 12, 20, 30, 42, 56, 72, 90 https://codegolf.stackexchange.com/a/11480
  • 76. What’s going on? multiplication = [] for i in range(10): multiplication.append(i * ++i)
  • 77. What’s going on? multiplication = [] for i in range(10): multiplication.append(i * ++i) Not a valid operation
  • 78. What’s going on? multiplication = [] for i in range(10): multiplication.append(i * ++i) Not a valid operation [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
  • 79. What’s going on? multiplication = [] for i in range(10): multiplication.append(i * ++i) Not a valid operation [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] for multiplication[i] in multiplication: Assigns each element to index 9
  • 80. What’s going on? multiplication = [] for i in range(10): multiplication.append(i * ++i) Not a valid operation [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] for multiplication[i] in multiplication: Assigns each element to index 9 [0, 1, 4, 9, 16, 25, 36, 49, 64, 0]
  • 81. What’s going on? multiplication = [] for i in range(10): multiplication.append(i * ++i) Not a valid operation [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] for multiplication[i] in multiplication: Assigns each element to index 9 [0, 1, 4, 9, 16, 25, 36, 49, 64, 1]
  • 82. What’s going on? multiplication = [] for i in range(10): multiplication.append(i * ++i) Not a valid operation [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] for multiplication[i] in multiplication: Assigns each element to index 9 [0, 1, 4, 9, 16, 25, 36, 49, 64, 4]
  • 83. What’s going on? multiplication = [] for i in range(10): multiplication.append(i * ++i) Not a valid operation [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] for multiplication[i] in multiplication: Assigns each element to index 9 [0, 1, 4, 9, 16, 25, 36, 49, 64, 64]
  • 84. What’s going on? multiplication = [] for i in range(10): multiplication.append(i * ++i) Not a valid operation [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] for multiplication[i] in multiplication: Assigns each element to index 9
  • 86. Some take-aways 1. Write readable code
  • 87. Some take-aways 1. Write readable code 2.Document/Comment your tricks and hacks
  • 88. Some take-aways 1. Write readable code 2.Document/Comment your tricks and hacks 3.Use static analysis and a good IDE
  • 89. Some take-aways 1. Write readable code 2.Document/Comment your tricks and hacks 3.Use static analysis and a good IDE 4.RTFM!