Skip to Content
Learn
Learn Python: Function Arguments
Using None as a Sentinel

So if we can’t use a list as a default argument for a function, what can we use? If we want an empty list, we can use None as a special value to indicate we did not receive anything. After we check whether an argument was provided we can instantiate a new list if it wasn’t.

def add_author(authors_books, current_books=None): if current_books is None: current_books = [] current_books.extend(authors_books) return current_books

In the above function, we accept current_books a value expected to be a list. But we don’t require it. If someone calls add_author() without giving an argument for current_books, we supply an empty list. This way multiple calls to add_author won’t include data from previous calls to add_author.

Instructions

1.

Update the function so that calls to update_order don’t have side-effects — no order should affect other orders.

Folder Icon

Sign up to start coding

Already have an account?