Хочу ещё раз на примере декоратора trace пояснить, какие типы декораторов
используются на практике и как они работают.
Общая структура декоратора и пример использования:
def trace(func):
def inner(*args, **kwargs):| struct chromNameCallbackContext | |
| /* Some stuff that the bPlusTree traverser needs for context. */ | |
| { | |
| struct bbiChromInfo *list; /* The list we are building. */ | |
| boolean isSwapped; /* Need to byte-swap things? */ | |
| }; | |
| static void chromNameCallback(void *context, void *key, int keySize, void *val, int valSize) | |
| /* Callback that captures chromInfo from bPlusTree. */ | |
| { |
| import time | |
| from matplotlib import pyplot as plt | |
| def timed(f, args, *, n_iter=100): | |
| acc = float("inf") | |
| for i in range(n_iter): | |
| t0 = time.perf_counter() | |
| f(*args) |
| import scala.{specialized => spec} | |
| package object foo { | |
| class ImplicitTest(val n: Nothing) extends AnyVal { | |
| def foo[@spec(Double) T](size: Int)(f: Int => T): Array[T] = { | |
| f(42) | |
| ??? | |
| } | |
| } | |
| } |
| >>> Error(abc.ABC, Exception): pass | |
| ... | |
| >>> EndOfTheWorld(Exception): pass | |
| ... | |
| >>> Error.register(EndOfTheWorld) | |
| <class '__main__.EndOfTheWorld'> | |
| >>> e = EndOfTheWorld() | |
| >>> isintance(e, Error) | |
| True | |
| >>> try: |
| >>> class A: | |
| ... def __init__(self, __foo): | |
| ... print(__foo) | |
| ... | |
| >>> dis.dis(A.__init__) | |
| 3 0 LOAD_GLOBAL 0 (print) | |
| 2 LOAD_FAST 1 (_A__foo) | |
| 4 CALL_FUNCTION 1 | |
| 6 POP_TOP | |
| 8 LOAD_CONST 0 (None) |