How do you define a variable that you can’t assign a value to yet? You use
None is a special value in Python. It is unique (there can’t be two different
Nones) and immutable (you can’t update
None or assign new attributes to it).
none_var = None if none_var: print("Hello!") else: print("Goodbye") # Prints "Goodbye"
None is falsy, meaning that it evaluates to
False in an
if statement, which is why the above code prints “Goodbye”.
None is also unique, which means that you can test if something is
None using the
# first we define session_id as None session_id = None if session_id is None: print("session ID is None!") # this prints out "session ID is None!" # we can assign something to session_id if active_session: session_id = active_session.id # but if there's no active_session, we don't send sensitive data if session_id is not None: send_sensitive_data(session_id)
Above we initialize our
None, then set our
session_id if there is an active session. Since
session_id could either be
None we check if
None before sending our sensitive data.
Grab a new review using
get_next_review(). Save the results into a variable called
Check if there is a
review by comparing it against
review contains a value that isn’t
None, submit it by calling the function
review as an argument.