SlideShare uma empresa Scribd logo
1 de 80
Baixar para ler offline
Investigating
Python Wats
Venmo
Hacker School
Recurse Center
!
@amygdalama
mathamy.com
WAT
Identity
Mutability
Scope
Identity
>>>	
  a	
  =	
  256	
  
>>>	
  b	
  =	
  256	
  
>>>	
  a	
  is	
  b	
  
???
>>>	
  a	
  =	
  256	
  
>>>	
  b	
  =	
  256	
  
>>>	
  a	
  is	
  b	
  
True
>>>	
  a	
  =	
  257	
  
>>>	
  b	
  =	
  257	
  
>>>	
  a	
  is	
  b	
  
???
>>>	
  a	
  =	
  257	
  
>>>	
  b	
  =	
  257	
  
>>>	
  a	
  is	
  b	
  
False
$	
  python	
  
!
!
-­‐5
…
255
256
-­‐5
…
255
256 a
>>>	
  a	
  =	
  256	
  
!
!
b
a
>>>	
  a	
  =	
  256	
  
>>>	
  b	
  =	
  256	
  
!
-­‐5
…
255
256
b
a
>>>	
  a	
  =	
  256	
  
>>>	
  b	
  =	
  256	
  
>>>	
  a	
  is	
  b	
  
True
-­‐5
…
255
256
a
…
255
256
…
257
>>>	
  a	
  =	
  257	
  
!
!
a
…
255
256
…
257
257 b
>>>	
  a	
  =	
  257	
  
>>>	
  b	
  =	
  257	
  
!
a
…
255
256
…
257
257 b
>>>	
  a	
  =	
  257	
  
>>>	
  b	
  =	
  257	
  
>>>	
  a	
  is	
  b	
  
False
>>>	
  a	
  =	
  257;	
  b	
  =	
  257	
  
>>>	
  a	
  is	
  b	
  
???
>>>	
  a	
  =	
  257;	
  b	
  =	
  257	
  
>>>	
  a	
  is	
  b	
  
True
>>>	
  a	
  =	
  257	
  
>>>	
  b	
  =	
  257	
  
>>>	
  a	
  is	
  b	
  
False	
  
!
>>>	
  a	
  =	
  257;	
  b	
  =	
  257	
  
>>>	
  a	
  is	
  b	
  
True
>>>	
  a	
  =	
  257	
  
>>>	
  
!
!
!
!
!
>>>	
  a	
  =	
  257	
  
>>>	
  source	
  =	
  "a	
  =	
  257"	
  
>>>	
  code_obj_a	
  =	
  compile(	
  
...	
  	
  	
  	
  	
  source=source,	
  
...	
  	
  	
  	
  	
  filename="",	
  
...	
  	
  	
  	
  	
  mode="exec")	
  
!
>>>	
  a	
  =	
  257	
  
>>>	
  source	
  =	
  "a	
  =	
  257"	
  
>>>	
  code_obj_a	
  =	
  compile(	
  
...	
  	
  	
  	
  	
  source=source,	
  
...	
  	
  	
  	
  	
  filename="",	
  
...	
  	
  	
  	
  	
  mode="exec")	
  
>>>	
  code_obj_a.co_consts	
  
(257,	
  None)
>>>	
  source	
  =	
  "b	
  =	
  257"	
  
>>>	
  code_obj_b	
  =	
  compile(	
  
...	
  	
  	
  	
  	
  source=source,	
  
...	
  	
  	
  	
  	
  filename="",	
  
...	
  	
  	
  	
  	
  mode="exec")	
  
!
>>>	
  source	
  =	
  "b	
  =	
  257"	
  
>>>	
  code_obj_b	
  =	
  compile(	
  
...	
  	
  	
  	
  	
  source=source,	
  
...	
  	
  	
  	
  	
  filename="",	
  
...	
  	
  	
  	
  	
  mode="exec")	
  
>>>	
  code_obj_b.co_consts	
  
(257,	
  None)
>>>	
  source	
  =	
  "a	
  =	
  257;	
  b	
  =	
  257"	
  
!
!
!
!
!
>>>	
  source	
  =	
  "a	
  =	
  257;	
  b	
  =	
  257"	
  
>>>	
  code_obj	
  =	
  compile(	
  	
  
...	
  	
  	
  	
  	
  source=source,	
  
...	
  	
  	
  	
  	
  filename="",	
  
...	
  	
  	
  	
  	
  mode="exec")	
  
!
>>>	
  source	
  =	
  "a	
  =	
  257;	
  b	
  =	
  257"	
  
>>>	
  code_obj	
  =	
  compile(	
  	
  
...	
  	
  	
  	
  	
  source=source,	
  
...	
  	
  	
  	
  	
  filename="",	
  
...	
  	
  	
  	
  	
  mode="exec")	
  
>>>	
  code_obj.co_consts	
  
(257,	
  None)
Mutability
>>>	
  row	
  =	
  [""]	
  *	
  3	
  
>>>	
  row	
  
['',	
  '',	
  '']	
  
!
!
!
!
>>>	
  row	
  =	
  [""]	
  *	
  3	
  
>>>	
  row	
  
['',	
  '',	
  '']	
  
>>>	
  board	
  =	
  [row]	
  *	
  3	
  
!
!
!
>>>	
  row	
  =	
  [""]	
  *	
  3	
  
>>>	
  row	
  
['',	
  '',	
  '']	
  
>>>	
  board	
  =	
  [row]	
  *	
  3	
  
>>>	
  board	
  
[['',	
  '',	
  ''],	
  
	
  ['',	
  '',	
  ''],	
  
	
  ['',	
  '',	
  '']]
>>>	
  board[0]	
  
['',	
  '',	
  '']	
  
!
>>>	
  board[0]	
  
['',	
  '',	
  '']	
  
>>>	
  board[0][0]	
  
''
>>>	
  board[0][0]	
  =	
  "X"	
  
!
!
!
>>>	
  board[0][0]	
  =	
  "X"	
  
>>>	
  board	
  
???	
  
!
>>>	
  board[0][0]	
  =	
  "X"	
  
>>>	
  board	
  
[['X',	
  '',	
  ''],	
  
	
  ['X',	
  '',	
  ''],	
  
	
  ['X',	
  '',	
  '']]
row
"" "" ""
>>>	
  row	
  =	
  [""]	
  *	
  3
>>>	
  board	
  =	
  [row]	
  *	
  3
row
board[0]
board[1]
board[2]
"" "" ""
>>>	
  board[0][0]	
  =	
  "X"
row
board[0]
board[1]
board[2]
"X" "" ""
Mutable Default
Arguments
def	
  append_cat(l=[]):	
  
!
!
!
!
!
!
def	
  append_cat(l=[]):	
  
	
  	
  	
  	
  l.append('cat')	
  
	
  	
  	
  	
  return	
  l	
  
!
!
!
!
def	
  append_cat(l=[]):	
  
	
  	
  	
  	
  l.append(‘cat’)	
  
	
  	
  	
  	
  return	
  l	
  
!
>>>	
  append_cat()	
  
???	
  
!
def	
  append_cat(l=[]):	
  
	
  	
  	
  	
  l.append(‘cat’)	
  
	
  	
  	
  	
  return	
  l	
  
!
>>>	
  append_cat()	
  
['cat']	
  
!
def	
  append_cat(l=[]):	
  
	
  	
  	
  	
  l.append(‘cat’)	
  
	
  	
  	
  	
  return	
  l	
  
!
>>>	
  append_cat()	
  
['cat']	
  
>>>	
  append_cat()	
  
???
def	
  append_cat(l=[]):	
  
	
  	
  	
  	
  l.append(‘cat’)	
  
	
  	
  	
  	
  return	
  l	
  
!
>>>	
  append_cat()	
  
['cat']	
  
>>>	
  append_cat()	
  
['cat',	
  'cat']
def	
  append_cat(l=[]):	
  
	
  	
  	
  	
  l.append(‘cat’)	
  
	
  	
  	
  	
  return	
  l	
  
!
>>>	
  append_cat.__defaults__	
  
???	
  
!
def	
  append_cat(l=[]):	
  
	
  	
  	
  	
  l.append(‘cat’)	
  
	
  	
  	
  	
  return	
  l	
  
!
>>>	
  append_cat.__defaults__	
  
([],)	
  
!
def	
  append_cat(l=[]):	
  
	
  	
  	
  	
  l.append('cat')	
  
	
  	
  	
  	
  return	
  l	
  
!
>>>	
  append_cat()	
  
>>>	
  append_cat.__defaults__	
  
???	
  
def	
  append_cat(l=[]):	
  
	
  	
  	
  	
  l.append('cat')	
  
	
  	
  	
  	
  return	
  l	
  
!
>>>	
  append_cat()	
  
>>>	
  append_cat.__defaults__	
  
(['cat'],)	
  
>>>	
  append_cat.__defaults__	
  
(['cat'],)	
  
!
!
>>>	
  append_cat.__defaults__	
  
(['cat'],)	
  
>>>	
  _[0].append('dragon')	
  
!
>>>	
  append_cat.__defaults__	
  
(['cat'],)	
  
>>>	
  _[0].append('dragon')	
  
>>>	
  append_cat()	
  
???
>>>	
  append_cat.__defaults__	
  
(['cat'],)	
  
>>>	
  _[0].append('dragon')	
  
>>>	
  append_cat()	
  
['cat',	
  'dragon',	
  'cat']
Scope
>>>	
  a	
  =	
  1	
  
!
!
!
!
>>>	
  a	
  =	
  1	
  
>>>	
  def	
  foo():	
  
...	
  	
  	
  	
  	
  return	
  a	
  
!
!
>>>	
  a	
  =	
  1	
  
>>>	
  def	
  foo():	
  
...	
  	
  	
  	
  	
  return	
  a	
  
!
>>>	
  foo()	
  
???
>>>	
  a	
  =	
  1	
  
>>>	
  def	
  foo():	
  
...	
  	
  	
  	
  	
  return	
  a	
  
!
>>>	
  foo()	
  
1
>>>	
  a	
  =	
  1	
  
>>>	
  def	
  foo():	
  
...	
  	
  	
  	
  	
  return	
  a	
  
>>>	
  foo()	
  
???
• locals()	
  	
  	
  	
  	
  #	
  {}	
  
!
!
>>>	
  a	
  =	
  1	
  
>>>	
  def	
  foo():	
  
...	
  	
  	
  	
  	
  return	
  a	
  
>>>	
  foo()	
  
???
• locals()	
  	
  	
  	
  	
  #	
  {}	
  
!
!
>>>	
  a	
  =	
  1	
  
>>>	
  def	
  foo():	
  
...	
  	
  	
  	
  	
  return	
  a	
  
>>>	
  foo()	
  
???
• locals()	
  	
  	
  	
  	
  #	
  {}	
  
• enclosing	
  	
  	
  	
  #	
  {}	
  
!
>>>	
  a	
  =	
  1	
  
>>>	
  def	
  foo():	
  
...	
  	
  	
  	
  	
  return	
  a	
  
>>>	
  foo()	
  
???
• locals()	
  	
  	
  	
  	
  #	
  {}	
  
• enclosing	
  	
  	
  	
  #	
  {}	
  
!
>>>	
  a	
  =	
  1	
  
>>>	
  def	
  foo():	
  
...	
  	
  	
  	
  	
  return	
  a	
  
>>>	
  foo()	
  
???
• locals()	
  	
  	
  	
  	
  #	
  {}	
  
• enclosing	
  	
  	
  	
  #	
  {}	
  
• globals()	
  	
  	
  	
  #	
  {'a':	
  1}	
  
>>>	
  a	
  =	
  1	
  
>>>	
  def	
  foo():	
  
...	
  	
  	
  	
  	
  return	
  a	
  
>>>	
  foo()	
  
???
• locals()	
  	
  	
  	
  	
  #	
  {}	
  
• enclosing	
  	
  	
  	
  #	
  {}	
  
✓ globals()	
  	
  	
  	
  #	
  {'a':	
  1}	
  
>>>	
  a	
  =	
  1	
  
>>>	
  def	
  foo():	
  
...	
  	
  	
  	
  	
  return	
  a	
  
>>>	
  foo()	
  
1
• locals()	
  	
  	
  	
  	
  #	
  {}	
  
• enclosing	
  	
  	
  	
  #	
  {}	
  
✓ globals()	
  	
  	
  	
  #	
  {'a':	
  1}	
  
• builtins	
  	
  	
  	
  	
  #	
  {'True':	
  True,…}
>>>	
  a	
  =	
  1	
  
>>>	
  def	
  foo():	
  
...	
  	
  	
  	
  	
  return	
  a	
  
>>>	
  foo()	
  
1
>>>	
  a	
  =	
  1	
  
>>>	
  def	
  foo():	
  
...	
  	
  	
  	
  	
  return	
  a	
  
!
>>>	
  from	
  dis	
  import	
  dis	
  
!
!
>>>	
  a	
  =	
  1	
  
>>>	
  def	
  foo():	
  
...	
  	
  	
  	
  	
  return	
  a	
  
!
>>>	
  from	
  dis	
  import	
  dis	
  
>>>	
  dis(foo)	
  
	
  	
  2	
  	
  	
  	
  	
  	
  	
  0	
  LOAD_GLOBAL	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  0	
  (a)	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  3	
  RETURN_VALUE	
  	
  	
  
>>>	
  a	
  =	
  1	
  
>>>	
  def	
  foo():	
  
...	
  	
  	
  	
  	
  a	
  +=	
  1	
  
...	
  	
  	
  	
  	
  return	
  a	
  
!
>>>	
  foo()	
  
???
>>>	
  a	
  =	
  1	
  
>>>	
  def	
  foo():	
  
...	
  	
  	
  	
  	
  a	
  +=	
  1	
  
...	
  	
  	
  	
  	
  return	
  a	
  
!
>>>	
  foo()	
  
UnboundLocalError:	
  local	
  
variable	
  'a'	
  referenced	
  
before	
  assignment
“When you make an
assignment to a variable in a
scope, that variable becomes
local to that scope.”
>>>	
  a	
  =	
  1	
  
>>>	
  def	
  foo():	
  
...	
  	
  	
  	
  	
  #	
  a	
  +=	
  1	
  
...	
  	
  	
  	
  	
  a	
  =	
  a	
  +	
  1	
  
...	
  	
  	
  	
  	
  return	
  a
>>>	
  a	
  =	
  1	
  
>>>	
  def	
  foo():	
  
...	
  	
  	
  	
  	
  #	
  a	
  +=	
  1	
  
...	
  	
  	
  	
  	
  a	
  =	
  a	
  +	
  1	
  
...	
  	
  	
  	
  	
  return	
  a
>>>	
  a	
  =	
  1	
  
>>>	
  def	
  foo():	
  
...	
  	
  	
  	
  	
  #	
  a	
  +=	
  1	
  
...	
  	
  	
  	
  	
  a	
  =	
  a	
  +	
  1	
  
...	
  	
  	
  	
  	
  return	
  a
>>>	
  a	
  =	
  1	
  
>>>	
  def	
  foo():	
  
...	
  	
  	
  	
  	
  a	
  +=	
  1	
  
...	
  	
  	
  	
  	
  return	
  a	
  
...	
  	
  
>>>	
  dis(foo)	
  
	
  	
  2	
  	
  	
  	
  	
  	
  	
  0	
  LOAD_FAST	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  0	
  (a)	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  3	
  LOAD_CONST	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  1	
  (1)	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  6	
  INPLACE_ADD	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  7	
  STORE_FAST	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  0	
  (a)	
  	
  
!
	
  	
  3	
  	
  	
  	
  	
  	
  10	
  LOAD_FAST	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  0	
  (a)	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  13	
  RETURN_VALUE	
  	
  	
  	
  	
  	
  
>>>	
  a	
  =	
  1	
  
>>>	
  def	
  foo():	
  
...	
  	
  	
  	
  	
  a	
  +=	
  1	
  
...	
  	
  	
  	
  	
  return	
  a	
  
...	
  	
  
>>>	
  dis(foo)	
  
	
  	
  2	
  	
  	
  	
  	
  	
  	
  0	
  LOAD_FAST	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  0	
  (a)	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  3	
  LOAD_CONST	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  1	
  (1)	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  6	
  INPLACE_ADD	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  7	
  STORE_FAST	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  0	
  (a)	
  	
  
!
	
  	
  3	
  	
  	
  	
  	
  	
  10	
  LOAD_FAST	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  0	
  (a)	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  13	
  RETURN_VALUE	
  	
  	
  	
  	
  	
  
“Knowledge is power —
it’s measured in wats.”	

!
- Rose Ames
Thank you!
@amygdalama
mathamy.com
Links
• https://www.destroyallsoftware.com/talks/wat	

• http://akaptur.github.io/blog/2013/10/29/a-python-puzzle/	

• https://docs.python.org/3.4/c-api/long.html	

• https://docs.python.org/3/reference/
compound_stmts.html#function-definitions	

• http://effbot.org/zone/default-values.htm	

• https://docs.python.org/3/reference/
executionmodel.html#naming-and-binding	

• http://eli.thegreenplace.net/2011/05/15/understanding-
unboundlocalerror-in-python/	

• http://rose.github.io/posts/measured-in-wats/

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Hidden Gems of Ruby 1.9
Hidden Gems of Ruby 1.9Hidden Gems of Ruby 1.9
Hidden Gems of Ruby 1.9
 
Intro to OTP in Elixir
Intro to OTP in ElixirIntro to OTP in Elixir
Intro to OTP in Elixir
 
Programming Haskell Chapter8
Programming Haskell Chapter8Programming Haskell Chapter8
Programming Haskell Chapter8
 
Down the rabbit hole, profiling in Django
Down the rabbit hole, profiling in DjangoDown the rabbit hole, profiling in Django
Down the rabbit hole, profiling in Django
 
PubNative Tracker
PubNative TrackerPubNative Tracker
PubNative Tracker
 
Ruby on Rails: Tasty Burgers
Ruby on Rails: Tasty BurgersRuby on Rails: Tasty Burgers
Ruby on Rails: Tasty Burgers
 
The Ring programming language version 1.6 book - Part 48 of 189
The Ring programming language version 1.6 book - Part 48 of 189The Ring programming language version 1.6 book - Part 48 of 189
The Ring programming language version 1.6 book - Part 48 of 189
 
Use cases in the code with AOP
Use cases in the code with AOPUse cases in the code with AOP
Use cases in the code with AOP
 
Tame cloud complexity with F#-powered DSLs
Tame cloud complexity with F#-powered DSLsTame cloud complexity with F#-powered DSLs
Tame cloud complexity with F#-powered DSLs
 
穏やかにファイルを削除する
穏やかにファイルを削除する穏やかにファイルを削除する
穏やかにファイルを削除する
 
Combinator parsing
Combinator parsingCombinator parsing
Combinator parsing
 
Five
FiveFive
Five
 
Serhii Korolenko - Passing Security By
Serhii Korolenko - Passing Security BySerhii Korolenko - Passing Security By
Serhii Korolenko - Passing Security By
 
Elegant APIs
Elegant APIsElegant APIs
Elegant APIs
 
Perl6 grammars
Perl6 grammarsPerl6 grammars
Perl6 grammars
 
The Ring programming language version 1.6 book - Part 185 of 189
The Ring programming language version 1.6 book - Part 185 of 189The Ring programming language version 1.6 book - Part 185 of 189
The Ring programming language version 1.6 book - Part 185 of 189
 
Unbreakable: The Craft of Code
Unbreakable: The Craft of CodeUnbreakable: The Craft of Code
Unbreakable: The Craft of Code
 
The bones of a nice Python script
The bones of a nice Python scriptThe bones of a nice Python script
The bones of a nice Python script
 
The Art, Joy, and Power of Creating Musical Programs (JFugue at SXSW Interact...
The Art, Joy, and Power of Creating Musical Programs (JFugue at SXSW Interact...The Art, Joy, and Power of Creating Musical Programs (JFugue at SXSW Interact...
The Art, Joy, and Power of Creating Musical Programs (JFugue at SXSW Interact...
 
... now write an interpreter (PHPem 2016)
... now write an interpreter (PHPem 2016)... now write an interpreter (PHPem 2016)
... now write an interpreter (PHPem 2016)
 

Semelhante a Investigating Python Wats

Slides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammersSlides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammers
Giovanni924
 
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!..."A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
akaptur
 

Semelhante a Investigating Python Wats (20)

Intro
IntroIntro
Intro
 
Slides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammersSlides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammers
 
λ | Lenses
λ | Lensesλ | Lenses
λ | Lenses
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python Programming
 
Ruby - Uma Introdução
Ruby - Uma IntroduçãoRuby - Uma Introdução
Ruby - Uma Introdução
 
JVMLS 2016. Coroutines in Kotlin
JVMLS 2016. Coroutines in KotlinJVMLS 2016. Coroutines in Kotlin
JVMLS 2016. Coroutines in Kotlin
 
A Taste of Python - Devdays Toronto 2009
A Taste of Python - Devdays Toronto 2009A Taste of Python - Devdays Toronto 2009
A Taste of Python - Devdays Toronto 2009
 
Python Workshop
Python  Workshop Python  Workshop
Python Workshop
 
Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)
 
ScotRuby - Dark side of ruby
ScotRuby - Dark side of rubyScotRuby - Dark side of ruby
ScotRuby - Dark side of ruby
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
 
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPythonByterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
 
replacing `import` with `accio`
replacing `import` with `accio`replacing `import` with `accio`
replacing `import` with `accio`
 
Ruby closures, how are they possible?
Ruby closures, how are they possible?Ruby closures, how are they possible?
Ruby closures, how are they possible?
 
Parse Everything With Elixir
Parse Everything With ElixirParse Everything With Elixir
Parse Everything With Elixir
 
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!..."A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
 
RedDot Ruby Conf 2014 - Dark side of ruby
RedDot Ruby Conf 2014 - Dark side of ruby RedDot Ruby Conf 2014 - Dark side of ruby
RedDot Ruby Conf 2014 - Dark side of ruby
 
Perl 6 in Context
Perl 6 in ContextPerl 6 in Context
Perl 6 in Context
 
Python 1
Python 1Python 1
Python 1
 
Elixir -Tolerância a Falhas para Adultos - GDG Campinas
Elixir  -Tolerância a Falhas para Adultos - GDG CampinasElixir  -Tolerância a Falhas para Adultos - GDG Campinas
Elixir -Tolerância a Falhas para Adultos - GDG Campinas
 

Último

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Último (20)

WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 

Investigating Python Wats

  • 3. WAT
  • 6. >>>  a  =  256   >>>  b  =  256   >>>  a  is  b   ???
  • 7. >>>  a  =  256   >>>  b  =  256   >>>  a  is  b   True
  • 8. >>>  a  =  257   >>>  b  =  257   >>>  a  is  b   ???
  • 9. >>>  a  =  257   >>>  b  =  257   >>>  a  is  b   False
  • 11. -­‐5 … 255 256 a >>>  a  =  256   ! !
  • 12. b a >>>  a  =  256   >>>  b  =  256   ! -­‐5 … 255 256
  • 13. b a >>>  a  =  256   >>>  b  =  256   >>>  a  is  b   True -­‐5 … 255 256
  • 15. a … 255 256 … 257 257 b >>>  a  =  257   >>>  b  =  257   !
  • 16. a … 255 256 … 257 257 b >>>  a  =  257   >>>  b  =  257   >>>  a  is  b   False
  • 17. >>>  a  =  257;  b  =  257   >>>  a  is  b   ???
  • 18. >>>  a  =  257;  b  =  257   >>>  a  is  b   True
  • 19. >>>  a  =  257   >>>  b  =  257   >>>  a  is  b   False   ! >>>  a  =  257;  b  =  257   >>>  a  is  b   True
  • 20. >>>  a  =  257   >>>   ! ! ! ! !
  • 21. >>>  a  =  257   >>>  source  =  "a  =  257"   >>>  code_obj_a  =  compile(   ...          source=source,   ...          filename="",   ...          mode="exec")   !
  • 22. >>>  a  =  257   >>>  source  =  "a  =  257"   >>>  code_obj_a  =  compile(   ...          source=source,   ...          filename="",   ...          mode="exec")   >>>  code_obj_a.co_consts   (257,  None)
  • 23. >>>  source  =  "b  =  257"   >>>  code_obj_b  =  compile(   ...          source=source,   ...          filename="",   ...          mode="exec")   !
  • 24. >>>  source  =  "b  =  257"   >>>  code_obj_b  =  compile(   ...          source=source,   ...          filename="",   ...          mode="exec")   >>>  code_obj_b.co_consts   (257,  None)
  • 25. >>>  source  =  "a  =  257;  b  =  257"   ! ! ! ! !
  • 26. >>>  source  =  "a  =  257;  b  =  257"   >>>  code_obj  =  compile(     ...          source=source,   ...          filename="",   ...          mode="exec")   !
  • 27. >>>  source  =  "a  =  257;  b  =  257"   >>>  code_obj  =  compile(     ...          source=source,   ...          filename="",   ...          mode="exec")   >>>  code_obj.co_consts   (257,  None)
  • 29. >>>  row  =  [""]  *  3   >>>  row   ['',  '',  '']   ! ! ! !
  • 30. >>>  row  =  [""]  *  3   >>>  row   ['',  '',  '']   >>>  board  =  [row]  *  3   ! ! !
  • 31. >>>  row  =  [""]  *  3   >>>  row   ['',  '',  '']   >>>  board  =  [row]  *  3   >>>  board   [['',  '',  ''],    ['',  '',  ''],    ['',  '',  '']]
  • 32. >>>  board[0]   ['',  '',  '']   !
  • 33. >>>  board[0]   ['',  '',  '']   >>>  board[0][0]   ''
  • 34. >>>  board[0][0]  =  "X"   ! ! !
  • 35. >>>  board[0][0]  =  "X"   >>>  board   ???   !
  • 36. >>>  board[0][0]  =  "X"   >>>  board   [['X',  '',  ''],    ['X',  '',  ''],    ['X',  '',  '']]
  • 37. row "" "" "" >>>  row  =  [""]  *  3
  • 38. >>>  board  =  [row]  *  3 row board[0] board[1] board[2] "" "" ""
  • 39. >>>  board[0][0]  =  "X" row board[0] board[1] board[2] "X" "" ""
  • 42. def  append_cat(l=[]):          l.append('cat')          return  l   ! ! ! !
  • 43. def  append_cat(l=[]):          l.append(‘cat’)          return  l   ! >>>  append_cat()   ???   !
  • 44. def  append_cat(l=[]):          l.append(‘cat’)          return  l   ! >>>  append_cat()   ['cat']   !
  • 45. def  append_cat(l=[]):          l.append(‘cat’)          return  l   ! >>>  append_cat()   ['cat']   >>>  append_cat()   ???
  • 46. def  append_cat(l=[]):          l.append(‘cat’)          return  l   ! >>>  append_cat()   ['cat']   >>>  append_cat()   ['cat',  'cat']
  • 47. def  append_cat(l=[]):          l.append(‘cat’)          return  l   ! >>>  append_cat.__defaults__   ???   !
  • 48. def  append_cat(l=[]):          l.append(‘cat’)          return  l   ! >>>  append_cat.__defaults__   ([],)   !
  • 49. def  append_cat(l=[]):          l.append('cat')          return  l   ! >>>  append_cat()   >>>  append_cat.__defaults__   ???  
  • 50. def  append_cat(l=[]):          l.append('cat')          return  l   ! >>>  append_cat()   >>>  append_cat.__defaults__   (['cat'],)  
  • 52. >>>  append_cat.__defaults__   (['cat'],)   >>>  _[0].append('dragon')   !
  • 53. >>>  append_cat.__defaults__   (['cat'],)   >>>  _[0].append('dragon')   >>>  append_cat()   ???
  • 54. >>>  append_cat.__defaults__   (['cat'],)   >>>  _[0].append('dragon')   >>>  append_cat()   ['cat',  'dragon',  'cat']
  • 55. Scope
  • 56. >>>  a  =  1   ! ! ! !
  • 57. >>>  a  =  1   >>>  def  foo():   ...          return  a   ! !
  • 58. >>>  a  =  1   >>>  def  foo():   ...          return  a   ! >>>  foo()   ???
  • 59. >>>  a  =  1   >>>  def  foo():   ...          return  a   ! >>>  foo()   1
  • 60. >>>  a  =  1   >>>  def  foo():   ...          return  a   >>>  foo()   ???
  • 61. • locals()          #  {}   ! ! >>>  a  =  1   >>>  def  foo():   ...          return  a   >>>  foo()   ???
  • 62. • locals()          #  {}   ! ! >>>  a  =  1   >>>  def  foo():   ...          return  a   >>>  foo()   ???
  • 63. • locals()          #  {}   • enclosing        #  {}   ! >>>  a  =  1   >>>  def  foo():   ...          return  a   >>>  foo()   ???
  • 64. • locals()          #  {}   • enclosing        #  {}   ! >>>  a  =  1   >>>  def  foo():   ...          return  a   >>>  foo()   ???
  • 65. • locals()          #  {}   • enclosing        #  {}   • globals()        #  {'a':  1}   >>>  a  =  1   >>>  def  foo():   ...          return  a   >>>  foo()   ???
  • 66. • locals()          #  {}   • enclosing        #  {}   ✓ globals()        #  {'a':  1}   >>>  a  =  1   >>>  def  foo():   ...          return  a   >>>  foo()   1
  • 67. • locals()          #  {}   • enclosing        #  {}   ✓ globals()        #  {'a':  1}   • builtins          #  {'True':  True,…} >>>  a  =  1   >>>  def  foo():   ...          return  a   >>>  foo()   1
  • 68. >>>  a  =  1   >>>  def  foo():   ...          return  a   ! >>>  from  dis  import  dis   ! !
  • 69. >>>  a  =  1   >>>  def  foo():   ...          return  a   ! >>>  from  dis  import  dis   >>>  dis(foo)      2              0  LOAD_GLOBAL                    0  (a)                        3  RETURN_VALUE      
  • 70. >>>  a  =  1   >>>  def  foo():   ...          a  +=  1   ...          return  a   ! >>>  foo()   ???
  • 71. >>>  a  =  1   >>>  def  foo():   ...          a  +=  1   ...          return  a   ! >>>  foo()   UnboundLocalError:  local   variable  'a'  referenced   before  assignment
  • 72. “When you make an assignment to a variable in a scope, that variable becomes local to that scope.”
  • 73. >>>  a  =  1   >>>  def  foo():   ...          #  a  +=  1   ...          a  =  a  +  1   ...          return  a
  • 74. >>>  a  =  1   >>>  def  foo():   ...          #  a  +=  1   ...          a  =  a  +  1   ...          return  a
  • 75. >>>  a  =  1   >>>  def  foo():   ...          #  a  +=  1   ...          a  =  a  +  1   ...          return  a
  • 76. >>>  a  =  1   >>>  def  foo():   ...          a  +=  1   ...          return  a   ...     >>>  dis(foo)      2              0  LOAD_FAST                        0  (a)                        3  LOAD_CONST                      1  (1)                        6  INPLACE_ADD                                          7  STORE_FAST                      0  (a)     !    3            10  LOAD_FAST                        0  (a)                      13  RETURN_VALUE            
  • 77. >>>  a  =  1   >>>  def  foo():   ...          a  +=  1   ...          return  a   ...     >>>  dis(foo)      2              0  LOAD_FAST                        0  (a)                        3  LOAD_CONST                      1  (1)                        6  INPLACE_ADD                                          7  STORE_FAST                      0  (a)     !    3            10  LOAD_FAST                        0  (a)                      13  RETURN_VALUE            
  • 78. “Knowledge is power — it’s measured in wats.” ! - Rose Ames
  • 80. Links • https://www.destroyallsoftware.com/talks/wat • http://akaptur.github.io/blog/2013/10/29/a-python-puzzle/ • https://docs.python.org/3.4/c-api/long.html • https://docs.python.org/3/reference/ compound_stmts.html#function-definitions • http://effbot.org/zone/default-values.htm • https://docs.python.org/3/reference/ executionmodel.html#naming-and-binding • http://eli.thegreenplace.net/2011/05/15/understanding- unboundlocalerror-in-python/ • http://rose.github.io/posts/measured-in-wats/