fork download
  1. def f(x):
  2. def g():
  3. return x
  4. return g
  5.  
  6. g1, g2 = f(1), f(2)
  7.  
  8. # Equal code, inequivalent functions
  9. print g1.func_code == g2.func_code
  10. print g1.func_code.co_code == g2.func_code.co_code
  11. print g1() == g2()
  12.  
  13. print
  14.  
  15. def f(x):
  16. y = x**2 + 1
  17. return y
  18.  
  19. def g(x):
  20. a = x**2
  21. b = a + 1
  22. return b
  23.  
  24. # Unequal code, equivalent functions
  25. print f.func_code == g.func_code
  26. print f.func_code.co_code == g.func_code.co_code
  27.  
  28. print
  29.  
  30. def f():
  31. return 1
  32.  
  33. def g():
  34. return 2
  35.  
  36. # Just to show that comparing co_code in particular is extremely wrong.
  37.  
  38. print f.func_code.co_code == g.func_code.co_code
Success #stdin #stdout 0.01s 9016KB
stdin
Standard input is empty
stdout
True
True
False

False
False

True