mirror of
https://github.com/Fechin/reference.git
synced 2026-11-20 21:23:11 +03:00
add: @decorator
This commit is contained in:
1 parent
dd5718648b
commit
694a53af42
1 file changed
+21
@@ -1105,7 +1105,28 @@ add(5, 20) # => 25
|
||||
# => 5
|
||||
(lambda x, y: x ** 2 + y ** 2)(2, 1)
|
||||
```
|
||||
### @decorator {.col-span-2}
|
||||
|
||||
```python
|
||||
# Modify or extend behavior of function or class method,
|
||||
# without changing their actual code.
|
||||
|
||||
# Define decorator that will wrap function or method
|
||||
def handle_errors(func):
|
||||
def wrapper(*args, **kwargs):
|
||||
try:
|
||||
return func(*args, **kwargs)
|
||||
except Exception as e:
|
||||
return print(f"Error : {e}")
|
||||
return wrapper
|
||||
|
||||
# Decorate function or method
|
||||
@handle_errors
|
||||
def divide(a, b):
|
||||
return a / b
|
||||
|
||||
divide(10, 0) # Output : Error : division by zero
|
||||
```
|
||||
## Python Modules
|
||||
|
||||
### Import modules
|
||||
|
||||
Reference in new issue
Block a user