top of page

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

functools

  • Dec 25, 2018
  • 1 min read

הספריה 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 עושה פעולה מתמטית שיכולה לעתים לקצר תהליכים ולהיות מאוד שימושית. כידוע אנו יכולים לקבל מנה של שני מספרים...

 
 
 

8 Comments


Thanks for this clear and helpful explanation of functools — it’s great to see examples that make decorators and higher-order functions feel much more approachable. Concepts like these can be tricky at first, but your breakdown makes it easier to see how they help keep code clean and reusable. I especially appreciate the real code snippets — they really bring the ideas to life and show how small tools in the standard library can solve practical problems. When I’m taking a break from coding and want something a bit more relaxed, I often enjoy simulation games like <a href="https://busimulatorindomodapk.id/">Bus Simulator Indonesia uNLIMITED MONEY</a> or <a href="https://getbusimultimateapk.com/">Bus Simulator Ultimate MOD APK GAME</a> — both offer immersive and stress-free gameplay. Thanks again for the insightful…

Like

This is a clear and helpful explanation of how functools.reduce works. What’s interesting is that the same kind of logic is often used in real applications, including simulation games. In titles like Bus Simulator Indonesia (Latest Version 2026), similar calculations can be applied to things like total earnings, fuel consumption, or performance data by combining values step by step. It’s a good reminder that these Python concepts have very practical, real-world use cases.

Like

Jassica Ella
Jassica Ella
Dec 15, 2025

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
Dec 15, 2025

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
Dec 13, 2025

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
Single Post: Blog_Single_Post_Widget
bottom of page