SLIDE 1 On t the Usa Usage o
Pyth thonic I c Idioms ms
Carol V. Alexandru¹, José J. Merchante², Sebastiano Panichella¹,³ Sebastian Proksch¹, Harald C. Gall¹, Gregorio Robles² ¹Software Evolution and Architecture Lab, University of Zurich, Switzerland
{alexandru,proksch,gall}@ifi.uzh.ch
²Grupo de Sistemas y Comunicaciones, Universidad Rey Juan Carlos, Spain
jj.merchante@alumnos.urjc.es, grex@gsyc.urjc.es
³Service Prototype Lab, Zurich University of Applied Sciences, Switzerland
panc@zhaw.ch
Onward! 2018 Boston Park Plaza Hotel Boston, Massachusetts, United States 07 November 2018
SLIDE 2
SLIDE 3 Harald C. Gall Sebastiano Panichella Carol V. Alexandru Sebastian Proksch Gregorio Robles José J. Merchante
SLIDE 4
4
Th Things t to k know a about t Pyth thon
SLIDE 5 5
Th Things t to k know a about t Pyth thon
Crea eated ted by y Guido van Rossum
SLIDE 6 6
Th Things t to k know a about t Pyth thon
Crea eated ted by y Guido van Rossum
– He is (was) the “Benevolent Dictator for Life” – Makes the final decisions when necessary
SLIDE 7 7
Th Things t to k know a about t Pyth thon
Crea eated ted by y Guido van Rossum
– He is (was) the “Benevolent Dictator for Life” – Makes the final decisions when necessary
trong principles es
– The “Zen of Python” >>> import this
SLIDE 8
8
Th Things t to k know a about t Pyth thon
SLIDE 9 9
Th Things t to k know a about t Pyth thon
Crea eated ted by y Guido van Rossum
– He is (was) the “Benevolent Dictator for Life” – Makes the final decisions when necessary
trong principles es
– The “Zen of Python” >>> import this – Python Enhancement Proposals (PEPs)
SLIDE 10 10
Th Things t to k know a about t Pyth thon
Crea eated ted by y Guido van Rossum
– He is (was) the “Benevolent Dictator for Life” – Makes the final decisions when necessary
trong principles es
– The “Zen of Python” >>> import this – Python Enhancement Proposals (PEPs)
Widesprea ead adopti tion across many y fi fiel elds
SLIDE 11
11 11
Py Pythonic a c appears to s to b be 'a th thing'
SLIDE 12
12
Py Pythonic a c appears to s to b be 'a th thing'
“It would be more pythonic to...”
SLIDE 13
13
A simp mple q question
SLIDE 14 14
T
r claims t ms to b be e “pythonic” c”... ...
SLIDE 15
15
.. ...but t 100s 0s of people se seem em sce ceptica cal
SLIDE 16
16
“Pythonic” c” beca cause of t the s style guide ( (PEP 8) PEP 8)
SLIDE 17 17
“Pythonic” c” beca cause o
performa rmance ce
SLIDE 18
18
Idioms ms and signaling
SLIDE 19
19
Idioms ms and signaling
SLIDE 20
20
And th there a are exceptions
SLIDE 21 21
Cr Creati tive use o
the word “Pyth thonic” c”
SLIDE 22
22
Is th s there a a d definiti tion f for “Pyth thonic” c”?
SLIDE 23
23
Is th s there a a d definiti tion f for “Pyth thonic” c”?
SLIDE 24
24
Is th s there a a d definiti tion f for “Pyth thonic” c”?
SLIDE 25
25
Is th s there a a d definiti tion f for “Pyth thonic” c”?
SLIDE 26 26
Pyth thonic c vs. N
thonic
SLIDE 27 27
Pyth thonic c vs. N
thonic
SLIDE 28 28
Pyth thonic c vs. N
thonic
SLIDE 29
29
Is th s there a a d definiti tion f for “Pyth thonic” c”?
So it' t's “Using Pyth ython-specific syn yntax x and concep epts ts”, right? But t what t do devel eveloper ers bel elieve? eve?
SLIDE 30 30
Let' t's ask k a f few d devel elopers
Interviews done
- in Person
- at a Python conference
- in Spain
- using open questions
SLIDE 31 31
Let' t's ask k a f few d devel elopers
Interviews done
- in Person
- at a Python conference
- in Spain
- using open questions
SLIDE 32
32
What t does P s Pythonic c me mean?
SLIDE 33
33
What t does P s Pythonic c me mean?
“makes code easier to understand and maintain” “elegant and readable code” “boosts readability and performance”
SLIDE 34
34
What t does P s Pythonic c me mean?
“makes code easier to understand and maintain” “using features provided by the language or standard library” “elegant and readable code” “boosts readability and performance”
SLIDE 35
35
What t does P s Pythonic c me mean?
“makes code easier to understand and maintain” “using features provided by the language or standard library” “elegant and readable code” “boosts readability and performance” “simply the most accepted way of writing python”
SLIDE 36
36
Usi Using Py Python i idioms ms != != Pyth thonic
SLIDE 37
37
Usi Using Py Python i idioms ms != != Pyth thonic
→ → Using idioms ms != = Pyth ythonic code → → Using idioms ms != = always mo more e readable
SLIDE 38 38
Novice ce vs. P
Novi
– Better style – Fewer lines of code
Pro:
– Using built-in functionality – Efficient execution
SLIDE 39 39
Novice ce vs. P
Novi
– Better style – Fewer lines of code – Using idioms
Pro:
– Using built-in functionality – Efficient execution – Writing elegant code
SLIDE 40 40
Novice ce vs. P
Novi
– Better style – Fewer lines of code – Using idioms – Simpler interpretation
Pro:
– Using built-in functionality – Efficient execution – Writing elegant code – Less concrete interpretation
SLIDE 41
41
Le Learn rning ' 'Pyth thonic' c'?
SLIDE 42
42
Le Learn rning ' 'Pyth thonic' c'?
“StackOverflow shows you multiple points of view of people, and you always learn.”
SLIDE 43
43
Le Learn rning ' 'Pyth thonic' c'?
“StackOverflow shows you multiple points of view of people, and you always learn.” “reading code in repositories of other projects” “saw them in documentation”
SLIDE 44
44
Le Learn rning ' 'Pyth thonic' c'?
“StackOverflow shows you multiple points of view of people, and you always learn.” “reading code in repositories of other projects” “from colleagues during code review” “saw them in documentation”
SLIDE 45
45
Le Learn rning ' 'Pyth thonic' c'?
“StackOverflow shows you multiple points of view of people, and you always learn.” “Becoming a pythonic programmer takes time” “My code became more pythonic year after year” “reading code in repositories of other projects” “from colleagues during code review” “saw them in documentation”
SLIDE 46
46
Le Learn rning ' 'Pyth thonic' c'?
“StackOverflow shows you multiple points of view of people, and you always learn.” “Becoming a pythonic programmer takes time” “My code became more pythonic year after year” “reading code in repositories of other projects” “from colleagues during code review” “saw them in documentation”
→ Pythonic not ta taught t in books or lec ectu tures → Seems eems to to cree eep in with th exp experien ence
SLIDE 47
47
Do Do y you ca care? ? Do Do your r peer ers ca care?
SLIDE 48
48
Do Do y you ca care? ? Do Do your r peer ers ca care?
“Pythonic code is positively viewed but not required”
SLIDE 49
49
Do Do y you ca care? ? Do Do your r peer ers ca care?
“Pythonic code is positively viewed but not required” “If you're a novice Python programmer, better focus on general programming skills”
SLIDE 50
50
Do Do y you ca care? ? Do Do your r peer ers ca care?
“Pythonic idioms can at least be used to measure a developer’s knowledge.” “Pythonic code is positively viewed but not required” “If you're a novice Python programmer, better focus on general programming skills”
SLIDE 51
51
Do Do y you ca care? ? Do Do your r peer ers ca care?
“Pythonic idioms can at least be used to measure a developer’s knowledge.” “Pythonic code is positively viewed but not required” “If you're a novice Python programmer, better focus on general programming skills” “If I learn a new idiom I add it to my toolbox and then when I touch something, I modify it and leave it better, but it’s not an obsession.
SLIDE 52
52
Do Do y you ca care? ? Do Do your r peer ers ca care?
“Pythonic idioms can at least be used to measure a developer’s knowledge.” “Pythonic code is positively viewed but not required” “If you're a novice Python programmer, better focus on general programming skills” “If I learn a new idiom I add it to my toolbox and then when I touch something, I modify it and leave it better, but it’s not an obsession.
→ Pythonic imp mporta tant, t, but t not fo forma mally → Pythonic signals exp exper ertise e and garners res espect
SLIDE 53
53
A ca cata talogue of Py Pythonic I c Idioms ms
SLIDE 54 54
A ca cata talogue of Py Pythonic I c Idioms ms
ed online e at http ttp://p /pyth ythonic.libres esoft. t.info fo/c /cata talogue
– including examples, references and benchmarks
SLIDE 55 55
A ca cata talogue of Py Pythonic I c Idioms ms
ed online e at http ttp://p /pyth ythonic.libres esoft. t.info fo/c /cata talogue
– including examples, references and benchmarks
Compiled ed fr from
– Several books (on learning and applying Python) – Online statements by influential and renowned Python developers
SLIDE 56 56
A ca cata talogue of Py Pythonic I c Idioms ms
ed online e at http ttp://p /pyth ythonic.libres esoft. t.info fo/c /cata talogue
– including examples, references and benchmarks
Compiled ed fr from
– Several books (on learning and applying Python) – Online statements by influential and renowned Python developers
Classified ed into to “per erfo forma mance” e” and “rea eadability” ty”
– performance measured using benchmarks
SLIDE 57
57
A ca cata talogue of Py Pythonic I c Idioms ms
SLIDE 58
58
Em Empiri rica cal stu tudy
SLIDE 59 59
Em Empiri rica cal study setu tup
t rec ecen ent t commi mit t in 1000 Pyth ython projec ects ts fr from m GitH tHub
– >1mb, sorted by stars, cleaned for books etc., no forks, not archived – 178,735 files, 38,505,577 lines of code
SLIDE 60 60
Em Empiri rica cal study setu tup
t rec ecen ent t commi mit t in 1000 Pyth ython projec ects ts fr from m GitH tHub
– >1mb, sorted by stars, cleaned for books etc., no forks, not archived – 178,735 files, 38,505,577 lines of code
Analyz yzed ed using LIS ISA A (http tp:// //t.uzh zh.ch/Fk)
– AST-based detection of all idioms
SLIDE 61 61
Em Empiri rica cal study setu tup
t rec ecen ent t commi mit t in 1000 Pyth ython projec ects ts fr from m GitH tHub
– >1mb, sorted by stars, cleaned for books etc., no forks, not archived – 178,735 files, 38,505,577 lines of code
Analyz yzed ed using LIS ISA A (http tp:// //t.uzh zh.ch/Fk)
– AST-based detection of all idioms
ences es in #p #projec ects and total occurence e count
SLIDE 62
62
Emp mpiri rica cal s study - a a quick ck look
Idiom # projects (out of 1000) # of occurences List comprehension 866 75,466 Generator expressions 709 33,038 Dict comprehension 146 796
SLIDE 63
63
Emp mpiri rica cal s study - a a quick ck look
Idiom # projects (out of 1000) # of occurences List comprehension 866 75,466 Generator expressions 709 33,038 Dict comprehension 146 796 Simple magic methods Intermediate magic methods Advanced magic methods
SLIDE 64
64
Emp mpiri rica cal s study - a a quick ck look
Idiom # projects (out of 1000) # of occurences List comprehension 866 75,466 Generator expressions 709 33,038 Dict comprehension 146 796 Simple magic methods Intermediate magic methods Advanced magic methods __nonzero__(self) Defines behavior for when bool() is called on an instance of your class. Should return True or False, depending on whether you would want to consider the instance to be True or False.
Magic methods
SLIDE 65
65
Emp mpiri rica cal s study - a a quick ck look
Idiom # projects (out of 1000) # of occurences List comprehension 866 75,466 Generator expressions 709 33,038 Dict comprehension 146 796 Simple magic methods 759 78,376 Intermediate magic methods 417 13,255 Advanced magic methods 190 2,613
SLIDE 66
66
Emp mpiri rica cal s study - a a quick ck look
Idiom # projects (out of 1000) # of occurences List comprehension 866 75,466 Generator expressions 709 33,038 Dict comprehension 146 796 Simple magic methods 759 78,376 Intermediate magic methods 417 13,255 Advanced magic methods 190 2,613
→ Mo More e in-depth th res esearch is need eded ed → Detec etecti ting anti-idioms ms is diffi fficult
SLIDE 67 67
Wh What t we l learned so far
ythonic” is imp mporta tant t - somehow
SLIDE 68 68
Wh What t we l learned so far
ythonic” is imp mporta tant t - somehow
ythonic” en encomp mpasses es mo more e th than just t programm mming idioms ms
SLIDE 69 69
Wh What t we l learned so far
ythonic” is imp mporta tant t - somehow
ythonic” en encomp mpasses es mo more e th than just t programm mming idioms ms
ythonic idioms”...
– makes you appear more knowledgeable – alone does not
- t necessarily make your code better or more pythonic
SLIDE 70 70 70
Wh What t we l learned so far
ythonic” is imp mporta tant t - somehow
ythonic” en encomp mpasses es mo more e th than just t programm mming idioms ms
ythonic idioms”...
– makes you appear more knowledgeable – alone does not
- t necessarily make your code better or more pythonic
- “Pyth
ythonic” does es not always ys mea mean “more e rea eadable” e” fo for eve everyo yone
SLIDE 71 71 71
Wh What t we l learned so far
ythonic” is imp mporta tant t - somehow
ythonic” en encomp mpasses es mo more e th than just t programm mming idioms ms
ythonic idioms”...
– makes you appear more knowledgeable – alone does not
- t necessarily make your code better or more pythonic
- “Pyth
ythonic” does es not always ys mea mean “more e rea eadable” e” fo for eve everyo yone
ythonic” is not t lea earned sys ystema temati tically
SLIDE 72 72 72
Questi stions r s rema main... ...
there e someth mething spec ecial in th the cultu ture e of f Pyth ython?
– The Zen of Python / “one way to do it” – Why are there no words like “Rubyist”, “Javanese” or “C#y”
SLIDE 73 73 73
Questi stions r s rema main... ...
there e someth mething spec ecial in th the cultu ture e of f Pyth ython?
– The Zen of Python / “one way to do it” – Why are there no words like “Rubyist”, “Javanese” or “C#y”
f “Pyth ythonic” is not t just t syn ynta tax x and idioms ms, what t is it?
SLIDE 74 74 74
Questi stions r s rema main... ...
there e someth mething spec ecial in th the cultu ture e of f Pyth ython?
– The Zen of Python / “one way to do it” – Why are there no words like “Rubyist”, “Javanese” or “C#y”
f “Pyth ythonic” is not t just t syn ynta tax x and idioms ms, what t is it?
es “Pyth ythonic” correl elate te with th code quality?
SLIDE 75 75 75
Questi stions r s rema main... ...
there e someth mething spec ecial in th the cultu ture e of f Pyth ython?
– The Zen of Python / “one way to do it” – Why are there no words like “Rubyist”, “Javanese” or “C#y”
f “Pyth ythonic” is not t just t syn ynta tax x and idioms ms, what t is it?
es “Pyth ythonic” correl elate te with th code quality?
t a posh ma mark of pride e ser erving to crea eate te a tw two-ti tier er society ety with thin th the e Pyth ython commu mmunity?
SLIDE 76 Carol V. Alexandru¹, José J. Merchante², Sebastiano Panichella¹,³ Sebastian Proksch¹, Harald C. Gall¹, Gregorio Robles²
Read the paper: http://t.uzh.ch/S7 Get the slides: http://t.uzh.ch/Sb Browse the catalogue: http://pythonic.libresoft.info/catalogue
On t the Usa Usage o
Pyth thonic I c Idioms ms