Skip to content

Instantly share code, notes, and snippets.

@nedbat
Created October 21, 2018 14:00
Show Gist options
  • Save nedbat/d603a34136299f0c0b8e442fccadeb7d to your computer and use it in GitHub Desktop.
Save nedbat/d603a34136299f0c0b8e442fccadeb7d to your computer and use it in GitHub Desktop.
Decorator tracing comparison

Decorator tracing comparison

dec0.py

1  def decorator(f):
2      return f
3
4  @decorator
5  def func():
6      print("hello")
7
8  func()
3.7 3.8 3.8 (pr9731)
 --- modulename: dec0, funcname: <module>
dec0.py(1): def decorator(f):
dec0.py(4): @decorator
 --- modulename: dec0, funcname: decorator
dec0.py(2):     return f
dec0.py(8): func()
 --- modulename: dec0, funcname: func
dec0.py(6):     print("hello")
hello
 --- modulename: dec0, funcname: <module>
dec0.py(1): def decorator(f):
dec0.py(4): @decorator
 --- modulename: dec0, funcname: decorator
dec0.py(2):     return f
dec0.py(8): func()
 --- modulename: dec0, funcname: func
dec0.py(6):     print("hello")
hello
 --- modulename: dec0, funcname: <module>
dec0.py(1): def decorator(f):
dec0.py(4): @decorator
dec0.py(5): def func():
 --- modulename: dec0, funcname: decorator
dec0.py(2):     return f
dec0.py(8): func()
 --- modulename: dec0, funcname: func
dec0.py(6):     print("hello")
hello

dec1.py

 1  def decorator(arg):
 2      def _dec(f):
 3          return f
 4      return _dec
 5
 6  @decorator(6)
 7  def my_function():
 8      x = 13
 9
10  my_function()
3.7 3.8 3.8 (pr9731)
 --- modulename: dec1, funcname: <module>
dec1.py(1): def decorator(arg):
dec1.py(6): @decorator(6)
 --- modulename: dec1, funcname: decorator
dec1.py(2):     def _dec(f):
dec1.py(4):     return _dec
 --- modulename: dec1, funcname: _dec
dec1.py(3):         return f
dec1.py(10): my_function()
 --- modulename: dec1, funcname: my_function
dec1.py(8):     x = 13
 --- modulename: dec1, funcname: <module>
dec1.py(1): def decorator(arg):
dec1.py(6): @decorator(6)
 --- modulename: dec1, funcname: decorator
dec1.py(2):     def _dec(f):
dec1.py(4):     return _dec
 --- modulename: dec1, funcname: _dec
dec1.py(3):         return f
dec1.py(10): my_function()
 --- modulename: dec1, funcname: my_function
dec1.py(8):     x = 13
 --- modulename: dec1, funcname: <module>
dec1.py(1): def decorator(arg):
dec1.py(6): @decorator(6)
 --- modulename: dec1, funcname: decorator
dec1.py(2):     def _dec(f):
dec1.py(4):     return _dec
dec1.py(7): def my_function():
 --- modulename: dec1, funcname: _dec
dec1.py(3):         return f
dec1.py(10): my_function()
 --- modulename: dec1, funcname: my_function
dec1.py(8):     x = 13

dec2.py

 1  def decorator1(f):
 2      return f
 3
 4  def decorator2(f):
 5      return f
 6
 7  def decorator3(f):
 8      return f
 9
10  @decorator1
11  @decorator2
12  @decorator3
13  def func():
14      print("hello")
15
16  func()
3.7 3.8 3.8 (pr9731)
 --- modulename: dec2, funcname: <module>
dec2.py(1): def decorator1(f):
dec2.py(4): def decorator2(f):
dec2.py(7): def decorator3(f):
dec2.py(10): @decorator1
dec2.py(11): @decorator2
dec2.py(12): @decorator3
 --- modulename: dec2, funcname: decorator3
dec2.py(8):     return f
 --- modulename: dec2, funcname: decorator2
dec2.py(5):     return f
 --- modulename: dec2, funcname: decorator1
dec2.py(2):     return f
dec2.py(16): func()
 --- modulename: dec2, funcname: func
dec2.py(14):     print("hello")
hello
 --- modulename: dec2, funcname: <module>
dec2.py(1): def decorator1(f):
dec2.py(4): def decorator2(f):
dec2.py(7): def decorator3(f):
dec2.py(10): @decorator1
dec2.py(11): @decorator2
dec2.py(12): @decorator3
dec2.py(10): @decorator1
 --- modulename: dec2, funcname: decorator3
dec2.py(8):     return f
 --- modulename: dec2, funcname: decorator2
dec2.py(5):     return f
 --- modulename: dec2, funcname: decorator1
dec2.py(2):     return f
dec2.py(16): func()
 --- modulename: dec2, funcname: func
dec2.py(14):     print("hello")
hello
 --- modulename: dec2, funcname: <module>
dec2.py(1): def decorator1(f):
dec2.py(4): def decorator2(f):
dec2.py(7): def decorator3(f):
dec2.py(10): @decorator1
dec2.py(11): @decorator2
dec2.py(12): @decorator3
dec2.py(13): def func():
 --- modulename: dec2, funcname: decorator3
dec2.py(8):     return f
 --- modulename: dec2, funcname: decorator2
dec2.py(5):     return f
 --- modulename: dec2, funcname: decorator1
dec2.py(2):     return f
dec2.py(16): func()
 --- modulename: dec2, funcname: func
dec2.py(14):     print("hello")
hello
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment