DEV Community

Bekzod
Bekzod

Posted on • Edited on

Python da decorator

Decorator larni tushunishdan avval Python da closure funksiyalar ko'rib chiqamiz.

def hello(name):
    # ichki funksiya
    def get_name():
        return f"Hello, {name}!"

    # ichki funksiyani qaytarish
    return get_name


hello_func = hello("Sarvar")
print(hello_func())  # Hello, Sarvar!
Enter fullscreen mode Exit fullscreen mode

Closure bu ichma-ich funksiyada tashqi funksiya o'zgaruvchilarini ichki funksiyada foydalanish mumkin bo'ladi.
Quyida ikkita sonni ko'paytiradigan closure yaratamiz.

def multiplier(n):
    def mul(x):
        return x * n

    return mul


# Multiplier of 3
mul_three = multiplier(3)

# Multiplier of 5
mul_five = multiplier(5)

print(mul_three(9))  # 27
print(mul_five(3))  # 15
print(mul_five(mul_three(2)))  # 30
Enter fullscreen mode Exit fullscreen mode

Decorator yaratishimiz da ham closure orqali amalga oshiramiz.
Decorator orqali boshqa funksiyada ishlatilgan holda uning xususiyatini o'zgartirishimiz mumkin bo'ladi.
Decorators
Manba: Medium

Decorator funksiya argument sifatida funksiya qabul qiladi.

def make_decorator(func):
    def inner():
        # Ichki funksiya orqali func xususiyatlarini o'zgartirishimiz mumkin
        print("Dekorator ishlayapti")

        func()

    # Ichki funksiya qaytariladi
    return inner


def my_func():
    print("Dekorator uchun ishlatiladigan funksiya")


decorated_func = make_decorator(my_func)
decorated_func()

# Dekorator ishlayapti
# Dekorator uchun ishlatiladigan funksiya
Enter fullscreen mode Exit fullscreen mode

Decorator funksiya @ orqali ishlatiladi.

@make_decorator
def my_func():
    print("Dekorator uchun ishlatiladigan funksiya")


my_func()

# Dekorator ishlayapti
# Dekorator uchun ishlatiladigan funksiya
Enter fullscreen mode Exit fullscreen mode

Funksiyani argumentlar orqali decoratorda ishlatishni ko'rib chiqamiz.
2 ta sonni bo'ladigan divider(a, b) funksiya yaratamiz.

def divider(a, b):
    return a / b


print(divider(10, 5))  # 2.0
print(divider(10, 0))  # ZeroDivisionError: division by zero
Enter fullscreen mode Exit fullscreen mode

b o'zgaruvchiga 0 qiymat beriladigan bo'lsa ZeroDevisionError xatolik qaytaradi, buni oldini olish uchun decorator funksiya yaratamiz.

def divide_decorator(func):
    def divide_inner(a, b):
        try:
            return func(a, b)
        except ZeroDivisionError:
            print("Nolga bo'lish mumkin emas!")

    return divide_inner


@divide_decorator
def divider(a, b):
    return a / b


print(divider(10, 5))  # 2.0
print(divider(10, 0))  # Nolga bo'lish mumkin emas!
Enter fullscreen mode Exit fullscreen mode

Bitta funksiyada bir nechta decorator ishlatishimiz ham mumkin, bunda birinchisidan boshlab decoratorlar ishlab keladi.

def divide_decorator(func):
    def divide_inner(a, b):
        try:
            return func(a, b)
        except ZeroDivisionError:
            print("Nolga bo'lish mumkin emas!")

    return divide_inner


def increment_arg_if_second_zero(func):
    def inner(a, b):
        if b == 0:
            b += 1
        return func(a, b)

    return inner


@increment_arg_if_second_zero
@divide_decorator
def divider(a, b):
    return a / b


print(divider(10, 5))  # 2.0
print(divider(10, 0))  # 10.0
Enter fullscreen mode Exit fullscreen mode

Keyingi qismlarda decorator lar bo'yicha murakkab funksiyalarni ko'rib chiqamiz.

Top comments (1)

Collapse
 
shohsanam_ergasheva1 profile image
SHOHSANAM ERGASHEVA

go on