Filename: Private: No Yes Filetype: Auto ABAP Sophia Apex Azure CLI Batch Bicep C Cameligo Clojure CoffeeScript C++ C# CSP CSS Cypher Dart Dockerfile ECL Elixir Flow9 FreeMarker2 FreeMarker2 (Angle/Bracket) FreeMarker2 (Angle/Dollar) FreeMarker2 (Auto/Bracket) FreeMarker2 (Auto/Dollar) FreeMarker2 (Bracket/Bracket) FreeMarker2 (Bracket/Dollar) F# Go GraphQL Handlebars Terraform HTML Ini Java JavaScript Julia Kotlin Less Lexon Liquid Lua Modula-3 Markdown MDX MIPS DAX MySQL Objective-C Pascal Pascaligo Perl PostgreSQL PHP Plain text ATS PQ PowerShell Protobuf Pug Python Q# R Razor Redis Redshift ReStructuredText Ruby Rust Small Basic Scala Scheme Sass Shell Solidity SPARQL SQL StructuredText Swift SV Tcl Twig TypeScript TypeSpec Visual Basic V WebGPU Shading Language XML YAML Indentation: Spaces Tabs 1 2 3 4 5 6 7 8 Clone #!/usr/bin/env python # pylint:disable=missing-module-docstring # pylint:disable=missing-function-docstring import inspect # # Simple decorator # def simple_decorator(func): def wrapper(*args, **kwargs): print('{{ wrapper()') result = func(*args, **kwargs) print('}}\n') return result return wrapper # The following: @simple_decorator def do_something_1(): print(inspect.currentframe().f_code.co_name) # Is the same as: def do_something_2(): print(inspect.currentframe().f_code.co_name) do_something_2 = simple_decorator(do_something_2) # # Parameterized decorator (a function that returns a decorator) # def parameterized_decorator(param=1337, other=13.37): def decorator(func): def wrapper(*args, **kwargs): print('{{ wrapper(param=%s, other=%s)' % (param, other)) result = func(*args, **kwargs) print('}}\n') return result return wrapper return decorator # The following: @parameterized_decorator() # Note the `()` - this is a "decorator-decorator" def do_something_3(): print(inspect.currentframe().f_code.co_name) # Is the same as: def do_something_4(): print(inspect.currentframe().f_code.co_name) do_something_4 = parameterized_decorator()(do_something_4) # # Parameterized decorator with defaults, working in mixed mode # def mixed_decorator(param=1337, other=13.37): def decorator(func): def wrapper(*args, **kwargs): print('{{ wrapper(param=%s, other=%s)' % (param, other)) result = func(*args, **kwargs) print('}}\n') return result return wrapper # Basically, if first parameter is a function, and all other parameters are equal to their default values, # one can assume that the decorator has been (mistakenly?) used in the code as: # @mixed_decorator # no `()` # def foo(): if callable(param) and other == 13.37: func, param = param, 1337 return decorator(func) return decorator @mixed_decorator def do_something_5(): print(inspect.currentframe().f_code.co_name) @mixed_decorator() def do_something_6(): print(inspect.currentframe().f_code.co_name) # # Test this shit # do_something_1() do_something_2() do_something_3() do_something_4() do_something_5() do_something_6() Paste