Scaricare Complete Python Developer In 2020: Zero To Mastery Lezioni -

say = greet # assign function to variable print(say("Alice")) # Hello, Alice def outer(msg): def inner(): # closure captures 'msg' print(msg) return inner

closure_func = outer("Secret") closure_func() # Secret def logger(func): def wrapper(*args, **kwargs): print(f"Calling {func. name }") result = func(*args, **kwargs) print(f"Finished {func. name }") return result return wrapper say = greet # assign function to variable

multiply(4, 7) from functools import wraps 5)) @logger def multiply(a

square(3) # Call 1 square(4) # Call 2 print(square.calls) # 2 import time say = greet # assign function to variable

def add(a, b): return a + b

add = logger(add) # manual decoration print(add(3, 5)) @logger def multiply(a, b): return a * b

def __call__(self, *args, **kwargs): self.calls += 1 print(f"Call {self.calls} of {self.func.__name__}") return self.func(*args, **kwargs) @CountCalls def square(x): return x * x

Scroll to Top
close