September 9th, 2005, 04:27 PM
Assert and Yield
Im a bit too lazy to go searching google for the purpose of these keywords. But can anyone please explain the meaning of the assert keyword and the yield keyword and an example of how it can be used? Any help is appreciated.
September 9th, 2005, 05:02 PM
assert is useful for debugging. You can use it to sanity check variables during the debugging stage. Basically, the following two are equivalent:
The code above will make sure x == 10, or throw an exception if the value is different from 10. This way, you can make sure that the value of x is set to the value you expect it to be, before the program proceeds further down the line and creates problems elsewhere due to the fact that x was not 10 when you expected it to be.
assert (x == 10)
is the same as:
if not (x == 10): raise AssertionError
The nice thing about using assert is that if the internal __debug__ system variable is not set, the assert statements will not be evaluated. Thus, your code runs a bit faster when you turn debug mode off, since the assert statements are skipped. In fact, compiled code might omit all the assert statements from the final executable.
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
"I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
September 9th, 2005, 05:20 PM
Ah. I see. Thank you very much Scorpions.
September 9th, 2005, 08:04 PM
Yield is used for generator functions.
When you use 'for', you iterate over items in a list, characters in a string, and so on ('sequences'), right? e.g.
Well, behind the scenes, you are getting an iterator, which gives you the next thing in the sequence, and calling it repeatedly.
for x in [1,2,3,5,4,7]:
What's happening is more like:
Well, a generator function is a function that generates a sequence - it behaves a bit like a list.
>>> x = [1,2,3].__iter__()
So instead of building a list all at once, and returning it, like so:
num_list = 
for i in range(3):
You can skip the temporary list, and return "the next value in the generated sequence", like this:
Then use it as if it was a list, like this:
for i in range(3):
It doesn't matter that give_me_numbers doesn't return a list. It returns a generator...
for num in give_me_numbers():
and you can use that in your for loop.
<generator object at 0x016E4C88>
'yield' is the keyword needed to do this in a function. Instead of 'return'.
You might wonder what the point is. Well, consider building a list of ten thousand items in a function, returning the list, and then stepping through each item.
1) Calculating a list of 10,000 complicated items might take minutes.
2) Storing all 10,000 at once will take a lot of memory.
Whereas if you used a generator function:
1) Each time the function is called, it only calculates the next item. Then it is returned and used. So there is no single big delay at the start waiting for the list to be built. It could be a lot more even on processor use, and the second part starts happening sooner.
2) The list is never all stored. One item is calculated, returned, used and then ready to clear up. Memory use is much, much lower.
September 10th, 2005, 09:37 AM
Wow. Yield will come in great handy. Thank you sfb for the very descriptive, detailed post.