Advertisement
FranzVuttke

decorators_demo.py

Nov 28th, 2023 (edited)
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.05 KB | Source Code | 0 0
  1. #!/usr/bin/python
  2.  
  3. # https://pastebin.com/MfJF9JdD
  4. import functools
  5.  
  6.  
  7. def polygon_square(func):
  8.     @functools.wraps(func)
  9.     def wrapper(*args, **kwargs):
  10.         result = func(*args, **kwargs)
  11.         # print(f"Pole pow.: ", end="")
  12.         for ind, arg in enumerate(args, 97): # chr(97) => 'a'
  13.             print(f"bok {chr(ind)} = {arg}" )
  14.         print(f"{result[0]} - pole pow.: {result[1]} ")
  15.         return result
  16.     return wrapper
  17.  
  18. @polygon_square
  19. def quadrat(side):
  20.     return ("kwadrat", side ** 2)
  21.  
  22. @polygon_square
  23. def rectangle(side_a, side_b):
  24.     return ("prostokÄ…t", side_a * side_b)
  25.  
  26.  
  27. quadrat(4)
  28. rectangle(3, 2)
  29.  
  30.  
  31. def animal_presentation(func):
  32.     @functools.wraps(func)
  33.  
  34.     def wrapper(*args, **kwargs):
  35.         print("Ten zwierzak to: ")
  36.         result = func(*args, *kwargs)
  37.         return result
  38.  
  39.     return wrapper
  40.  
  41.  
  42. @animal_presentation
  43. def dog():
  44.     print("piesek")
  45.     print("hau, hau!!!")
  46.  
  47. @animal_presentation
  48. def cat():
  49.     print("kotek")
  50.     print("mrau, mrau...")
  51.  
  52. # dog()
  53. # cat()
  54.  
  55.  
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement