From The Update Dept: So I fired up Radio tonight and tried to write something, but was too tired. Of course, when I was trying to go to sleep, I get two ideas/solutions-to-problems. One of them I present to you, gentle reader... |
From the Useless-Python-Tricks Dept: Here's something I whipped up when I was trying to go to bed..
def WDTestForResultWithGiven(expectedResult, function, *args, **keywords): """calls our function with the specified args or keyword args See Python Essential Reference, Second Edition, Chapter 6 for information on how this was done.""" Calling this routine would look something like this:
import string WDTestForResultWithGiven(-1, "h4ck3r".find, "boi") #will return 1 (true) The *args parameter tells Python to to add any extra parameters into a tuple, while the **keywords parameter tells Python to take any named parameters and put them in a dictionary. I'm not completely sure I'd want to use this in production code. First, while it is pretty neat, and shows the power of Python well, it makes two (somewhat expensive) function calls where there was once one ("expensive" being a relative word, of course). My function also seems go go against the Python Zen - and one of the principles says "simple is better than complex". It also breaks up the logic, making things harder to see. But it's a rather cool function - I'll probably keep it around - I may find a use for it, someday. Maybe. |