Date: 08 May 2015 14:34
Number of posts: 4
RSS: New posts
בשאלה 2 ב' עליכם להשתמש בפונקציית מעטפה.
binom_fast מהתרגול מהווה דוגמא לשימוש בפונקציית מעטפה.
למה בעצם חייבים להשתמש בפונקציית מעטפה?
אי אפשר פשוט לאתחל את המילון שורה מעל הפונקציה change_fast()?
You could initialize a dictionary object named the same as the function's dictionary, and in this case it would work perfectly fine. There are a few reasons why we prefer wrappers, though:
1. When you initialize the dictionary before the function, it belongs to the program's global scope. As a result, successive calls to the recursive function will share the same dictionary; in particular, the keys entered in the first call will remain there for the second call. Sometimes this is fine (and even saves some work), but in other recursive problems the same key may actually encode different sub-problems in different calls (initiated with different arguments).
2. More fundamentally, the dictionary is the function's business and nobody else's. It is a technical implementation detail that should be contained within the function's scope, instead of polluting the global space, possibly colliding with other dictionaries out there.
3. Even more generally, global variables should be used with caution, not just in the recursive context. They make the program's state unpredictable and the program itself unreadable. Google "global variables evil" and you'll find plenty of discussion about this.