SlideShare a Scribd company logo
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/

More Related Content

What's hot

Intro to OTP in Elixir
Intro to OTP in ElixirIntro to OTP in Elixir
Intro to OTP in Elixir
Jesse Anderson
 
Programming Haskell Chapter8
Programming Haskell Chapter8Programming Haskell Chapter8
Programming Haskell Chapter8
Kousuke Ruichi
 
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
Remco Wendt
 
PubNative Tracker
PubNative TrackerPubNative Tracker
PubNative Tracker
Andrew Djoga
 
Ruby on Rails: Tasty Burgers
Ruby on Rails: Tasty BurgersRuby on Rails: Tasty Burgers
Ruby on Rails: Tasty Burgers
Aaron Patterson
 
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
Mahmoud Samir Fayed
 
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
Andrzej Krzywda
 
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
Yan Cui
 
穏やかにファイルを削除する
穏やかにファイルを削除する穏やかにファイルを削除する
穏やかにファイルを削除する
鉄次 尾形
 
Combinator parsing
Combinator parsingCombinator parsing
Combinator parsing
Swanand Pagnis
 
Serhii Korolenko - Passing Security By
Serhii Korolenko - Passing Security BySerhii Korolenko - Passing Security By
Serhii Korolenko - Passing Security By
NoNameCon
 
Elegant APIs
Elegant APIsElegant APIs
Elegant APIs
Andrew Timberlake
 
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
Mahmoud Samir Fayed
 
Unbreakable: The Craft of Code
Unbreakable: The Craft of CodeUnbreakable: The Craft of Code
Unbreakable: The Craft of Code
Joe Morgan
 
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
saniac
 
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...
David Koelle
 
... now write an interpreter (PHPem 2016)
... now write an interpreter (PHPem 2016)... now write an interpreter (PHPem 2016)
... now write an interpreter (PHPem 2016)
James Titcumb
 

What's hot (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)
 

Similar to Investigating Python Wats

Slides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammersSlides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammersGiovanni924
 
λ | Lenses
λ | Lensesλ | Lenses
λ | Lenses
Open-IT
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python Programming
Muthu Vinayagam
 
Ruby - Uma Introdução
Ruby - Uma IntroduçãoRuby - Uma Introdução
Ruby - Uma Introdução
Ígor Bonadio
 
JVMLS 2016. Coroutines in Kotlin
JVMLS 2016. Coroutines in KotlinJVMLS 2016. Coroutines in Kotlin
JVMLS 2016. Coroutines in Kotlin
Andrey Breslav
 
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
Jordan Baker
 
Python Workshop
Python  Workshop Python  Workshop
Python Workshop
Assem CHELLI
 
Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)
Rick Copeland
 
ScotRuby - Dark side of ruby
ScotRuby - Dark side of rubyScotRuby - Dark side of ruby
ScotRuby - Dark side of ruby
Gautam Rege
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
UC San Diego
 
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
akaptur
 
replacing `import` with `accio`
replacing `import` with `accio`replacing `import` with `accio`
replacing `import` with `accio`
Amy Hanlon
 
Ruby closures, how are they possible?
Ruby closures, how are they possible?Ruby closures, how are they possible?
Ruby closures, how are they possible?
Carlos Alonso Pérez
 
Parse Everything With Elixir
Parse Everything With ElixirParse Everything With Elixir
Parse Everything With Elixir
Gabriele Lana
 
"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
 
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
Gautam Rege
 
Perl 6 in Context
Perl 6 in ContextPerl 6 in Context
Perl 6 in Context
lichtkind
 
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
Fabio Akita
 

Similar to 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
 

Recently uploaded

Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
Bhaskar Mitra
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
Product School
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
Fwdays
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 

Recently uploaded (20)

Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 

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/