#1
  1. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154

    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.
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    assert is useful for debugging. You can use it to sanity check variables during the debugging stage. Basically, the following two are equivalent:
    Code:
     
    assert (x == 10)
    
    is the same as:
    
    if __debug__:
        if not (x == 10): raise AssertionError
    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.

    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
  4. #3
  5. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Ah. I see. Thank you very much Scorpions.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    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.

    Code:
    for x in [1,2,3,5,4,7]:
        print x
    Well, behind the scenes, you are getting an iterator, which gives you the next thing in the sequence, and calling it repeatedly.

    What's happening is more like:

    Code:
    >>> x = [1,2,3].__iter__()
    >>> x.next()
    1
    >>> x.next()
    2
    >>> x.next()
    3
    >>>
    Well, a generator function is a function that generates a sequence - it behaves a bit like a list.

    So instead of building a list all at once, and returning it, like so:

    Code:
    def give_me_numbers():
        import random
        num_list = []
        
        for i in range(3):
            num_list.append(random.randint(1,10))
        
        return num_list

    You can skip the temporary list, and return "the next value in the generated sequence", like this:

    Code:
    def give_me_numbers():
        import random
    
        for i in range(3):
           yield random.randint(1,10)
    Then use it as if it was a list, like this:

    Code:
    for num in give_me_numbers():
        print num
    It doesn't matter that give_me_numbers doesn't return a list. It returns a generator...

    Code:
    >>> give_me_numbers()
    <generator object at 0x016E4C88>
    and you can use that in your for loop.

    '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.
  8. #5
  9. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Wow. Yield will come in great handy. Thank you sfb for the very descriptive, detailed post.

IMN logo majestic logo threadwatch logo seochat tools logo