top of page

רעננו את הדף והקליקו למעבר לנושא הבא:

functools

הספריה functools

הספריה functools מיועדת לפונקציות ממעלה גבוהה יותר, שזה אומר פונקציות הפועלות על פונקציות אחרות או המחזירות פונקציות. אחת המתודות הנפוצות שעברו לספריה הזאת לאחר שיצאו מהשפה הסטנדרטית בשל תחליפים ברורים יותר, היא functools.reduce המקבלת פונקציה ואובייקט איטרבילי, ומחזירה בסופו של דבר ערך אחד. בהתחלה היא מפעילה את הפונקציה על שני האיברים הראשונים, לאחר מכן, היא לוקחת את תוצאה שהתקבלה משני האיברים הראשונים ומפעילה את הפונקציה על התוצאה ועל האיבר השלישי, וכך הלאה, כל פעם לוקחת את התוצאה המצטברת ומפעילה על האיבר הבא, עד שמקבלים בסוף ערך אחד.

import operator,functools lista=[1,2,3,4,5] f=operator.mul print(functools.reduce(f,lista))

>>>

120

בדוגמא למעלה יצרנו פונקציה המכפילה שני איברים באמצעות המתודה mul המיובאת מהספריה operator אשר ניתן לקרוא אודותיה בהרחבה במדריך, ולקחנו רשימה פשוטה של המספרים 1-5 כעת מבצעים reduce (שהיא מתודה בספריה functools) שתכפיל את 1 ו- 2 לאחר מכן את התוצאה (2) ב- 3, לאחר מכן את התוצאה(6) ב- 4 , לאחר מכן את התוצאה (24) ב- 5 ובסוף מקבלים 120. בעצם בנינו מנגנון לחישוב עצרת!.


מתודה שימושית נוספת בספריית functools אודותיה כתבנו פוסט נפרד בשם memoization נקראת lru_cache והיא מסייעת בהאצת תוכניות עם פונקציות רקוסיביות מורכבות. תחפשו את הפוסט בבלוג ותקראו שם.

 
 
 

Related Posts

See All
divmod() - log עושים חשבון

מתודה נחמדה בפייתון נקראת divmod עושה פעולה מתמטית שיכולה לעתים לקצר תהליכים ולהיות מאוד שימושית. כידוע אנו יכולים לקבל מנה של שני מספרים...

 
 
 

6 Comments


Jassica Ella
Jassica Ella
10 hours ago

Functools shows how small functions combine to create powerful results, just like friendships built over time. Each memory adds value, forming something meaningful in the end. The same thoughtful approach applies when sharing Unique Birthday Wishes For Friends simple words, carefully chosen, come together to create heartfelt messages that truly celebrate friendship and lasting bonds.

Like

Professor
Professor
14 hours ago

Nice explanation of functools.reduce 

Just like reduce combines multiple elements into one final result, mod resources are often bundled together for games like Bus Simulator Indonesia Mod Apk—for example, a single bus simulator indonesia indian map obb file download packages maps and assets into one usable file. Different domains, but the same idea of efficient aggregation and reuse.

Like

Henry James
Henry James
3 days ago

The description of functools.reduce is on point. It works by repeatedly applying a specified function to the elements of an iterable, combining them into one cumulative result. In the given case, operator.mul successively multiplies the values together, showing why reduce is especially helpful for calculations and functional-style operations.

On a different note!

Beleza Sabor is a lifestyle platform focused on skincare, beauty, fashion products, and accessories, bringing together style and self-care in one place.

Like

Thomas
Thomas
Dec 07

Great explanation of how functools works! reduce() is especially powerful for turning a whole list into a single value through repeated operations. Your example with operator.mul is perfect—simple and clear. It’s always interesting to see how functional tools like this make Python code more expressive and compact.

Like

The explanation of functools.reduce is accurate—this function applies a given operation step by step across an iterable until a single final value is produced. In your example, operator.mul multiplies each number in the list with the accumulated result, which makes reduce very useful for mathematical and functional programming tasks. On another note, this page explains that FONE Chihuahua is a portal for teachers and school staff in Chihuahua to access their pay stubs and related documents securely through the SIE system.

Like
Single Post: Blog_Single_Post_Widget
bottom of page