Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!
This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join 11 million other learners and get started learning Python for data science today!
Good news! You can save 25% off your Datacamp annual subscription with the code LEARNPYTHON23ALE25 - Click here to redeem your discount
आंशिक फलन
You can create partial functions in python by using the partial function from the functools library.
आंशिक फ़ंक्शन मदद करते हैं एक फ़ंक्शन के x पैरामीटरों को एक ऐसे फ़ंक्शन में बदलने में जिसमें कम पैरामीटर होते हैं, और सीमित फ़ंक्शन के लिए निश्चित मान सेट किए गए होते हैं।
आवश्यक आयात:
from functools import partial
यह कोड 8 लौटाएगा।
from functools import partial
def multiply(x, y):
return x * y
# create a new function that multiplies by 2
dbl = partial(multiply, 2)
print(dbl(4))
एक महत्वपूर्ण नोट: डिफ़ॉल्ट मान बाएं से चर को बदलना शुरू कर देंगे। 2 x की जगह लेगा। y बराबर 4 होगा जब dbl(4) को कॉल किया जाएगा। इस उदाहरण में इसका कोई फर्क नहीं पड़ता, लेकिन यह नीचे के उदाहरण में करेगा।
Exercise
partial() को कॉल करते हुए प्रदान की गई फ़ंक्शन को संपादित करें और func() में पहले तीन चर को बदलें। फिर नए आंशिक फ़ंक्शन के साथ केवल एक इनपुट चर का उपयोग करते हुए प्रिंट करें ताकि आउटपुट 60 के बराबर हो।
#Following is the exercise, function provided:
from functools import partial
def func(u, v, w, x):
return u*4 + v*3 + w*2 + x
#Enter your code here to create and print with your partial function
from functools import partial
def func(u, v, w, x):
return u*4 + v*3 + w*2 + x
p = partial(func,5,6,7)
print(p(8))
#test_object('p')
test_output_contains('60')
success_msg('Good job!')
This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!