bslatkin / effectivepython

Effective Python: Second Edition — Source Code and Errata for the Book
https://effectivepython.com
2.2k stars 710 forks source link

Item 78: page 374, wrong function name: says `feed_func`, should be `feed_animal` #111

Open kirisakow opened 1 year ago

kirisakow commented 1 year ago

(2nd ed)

The code block in the middle of the page is about unittest.mock.patch and yet, instead of calling feed_animal(database, name, now), it calls feed_func(database, name, now) (a leftover of the precedent approach of using keyword-only arguments for injecting mocks).

Wrong code:

def do_rounds(database, species, *, utcnow=datetime.utcnow):
    now = utcnow()
    feeding_timedelta = get_food_period(database, species)
    animals = get_animals(database, species)
    fed = 0
    for name, last_mealtime in animals:
        if (now - last_mealtime) > feeding_timedelta:
            feed_func(database, name, now)        # wrong function name
            fed += 1
    return fed

Correct code:

def do_rounds(database, species, *, utcnow=datetime.utcnow):
    now = utcnow()
    feeding_timedelta = get_food_period(database, species)
    animals = get_animals(database, species)
    fed = 0
    for name, last_mealtime in animals:
        if (now - last_mealtime) > feeding_timedelta:
            feed_animal(database, name, now)        # correct function name
            fed += 1
    return fed
bslatkin commented 1 month ago

Thank you for the report! I also found this in the printed version of the book, but the example code doesn't have the same error:

https://github.com/bslatkin/effectivepython/blob/4ae6f3141291ea137eb29a245bf889dbc8091713/example_code/item_78.py#L150 https://github.com/bslatkin/effectivepython/blob/4ae6f3141291ea137eb29a245bf889dbc8091713/example_code/item_78.py#L264

Will resolve it.

bslatkin commented 1 month ago

Ah -- because it was fixed in #73