Python Projects for Beginners a ten-Week Bootcamp Approach to Python Programming



Download 2,61 Mb.
bet155/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   151   152   153   154   155   156   157   158   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

Higher-Order Functions


A higher-order function is a function that operates on other functions, either by taking a function as its argument or by returning a function. We saw this done in last week’s lesson with lambdas, map, filter, and reduce. Decorators are higher-order functions because they take in a function and return a function.

Creating and Applying a Decorator


We’ll need to declare a function that takes in another function as an argument in order to create a decorator. Inside of this decorator, we can then define another function to be returned that will run the function that was passed in as an argument. Let’s see how this is written:

1| # creating and applying our own decorator using the @ symbol 3| def decorator(func):
4| def wrap( ):
5| print("======")
6| func( )
7| print("======")
8| return wrap
10| @decorator 11| def printName( ):
12| print("John!")
14| printName( )

Go ahead and run the cell. We’ll get an output of “John!” with equal signs above and below the name that act as a border. On line 10 we attached our decorator to the printName function. Whenever the printName function is called, the decorator will run, and printName will be passed in as the argument of “func”. Within decorator we declare a function called wrap. This wrap function will print a border, then call the func argument, and then print another border. Remember that decorators must return a function in order to run. Our decorator that we declared can be attached to any function that we write. All functions with this decorator will simply run with a border above and below them.

Decorators with Parameters


Although decorators simply add extra capabilities to functions, they can also have arguments like any other function. Let’s take the following example where we want to run a function x times:

1| # creating a decorator that takes in parameters 3| def run_times(num):
4| def wrap(func):
5| for i in range(num):
6| func( )
7| return wrap
9| @run_times(4) 10| def sayHello( ):
11| print("Hello!")

Go ahead and run the cell. This cell will output “Hello!” four times. The syntax changes when the decorator accepts an argument. Our decorator this time accepted an argument of num, and the wrap function accepted the function as the argument this time. Within our wrap function, we created a for loop that would run the function attached to our decorator as many times as the argument declared on the decorator on line 9.
Note When passing an argument into a decorator, the function is automatically run, so we do not need to call sayhello in this instance.

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   151   152   153   154   155   156   157   158   ...   200




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish