Python



  • I'm studying the subject with the decorators, there's a mission. Condition: Announcing a function with a name get_sq, which calculates the area of a rectangle in two. Parameters: width and height - width and height of rectangle and return the result. Determine the decorator for this function with the name (external function) func_show, which displays the results on the screen in the form of a row (without skirt): _ Call for a decorated function get_sq.

    I've set up a code, like it's working, but I'm not sure it's right, because the cycle will cross all arguments, not just two: width and height. I'd like to know how the two arguments could be made. Is the decorator right? I would appreciate your explanations and advice. Thank you.

    def func_show(func):
    

    def get_sq(**kwargs):
    x = 1
    for v in kwargs.values():
    x *= int(v)
    func(x)
    return get_sq

    @func_show
    def result(kwargs):
    print(f"Площадь прямоугольника: {kwargs}")

    if name == 'main':
    result(width=4, height=6)



  • It's probably like this:

    def get_sq(width, height):
        return width * height
    

    def func_show(func):
    def decor(width, height):
    print(f"площадь прямоугольника: {func(width, height)}")

    return decor
    

    get_sq = func_show(get_sq)

    get_sq(4, 6)

    If there is a wish to retain the withdrawal as the original function:

    def get_sq(width, height):
    return width * height

    def func_show(func):
    def decor(width, height):
    square = func(width, height)
    print(f"площадь прямоугольника: {square}")
    return square

    return decor
    

    get_sq = func_show(get_sq)

    res = get_sq(4, 6)



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2