News for hackers. Stuff that matters.
Re:Storing Passwords in a Highly Parallelized World by mafro on Thursday January 07, 2016 @11:00AM (#10858636)
A core tenet of Python is that "it's easier to ask for forgiveness than permission":

https://docs.python.org/2/glossary.html#term-eafp

Re:Storing Passwords in a Highly Parallelized World by krallin on Thursday January 07, 2016 @11:22AM (#10858781)
This principle doesn't necessarily mean functions should never return booleans, though.

Booleans are used in a variety of (popular) Python libraries when checking whether a password is correct (e.g. Django's `check_password` returns False if the password is wrong).

Re:Storing Passwords in a Highly Parallelized World by lancefisher on Thursday January 07, 2016 @12:11PM (#10859170)
This is most likely the reason. It allows your code to follow the happy path for logging in, and treat verification as the exceptional case. Definitely a design choice by the author who self-identifies as a Pythonista.

  def handle_login():
    try:
      ph.verify(hash, "s3kr3tp4ssw0rd")
      log_login()
      redirect_to_page()
    except VerificationError:
      log_bad_login()
      redirect_to_login()
Re:Storing Passwords in a Highly Parallelized World by joshmaker on Thursday January 07, 2016 @01:12PM (#10859605)
While we're talking Python best practices, I highly recommend using an `else` clause to keep the code being 'excepted' to one line:

  def handle_login():
    try:
      ph.verify(hash, "s3kr3tp4ssw0rd")
    except VerificationError:
      log_bad_login()
      redirect_to_login()
    else:
      log_login()
      redirect_to_page()
It's not a big deal for this code, but in general this is good practice because

1. It makes it very obvious to the next developer which line is the one that is expected to raise that exception

2. One of the other lines could unintentionally raise that exception and mistakenly trigger the except clause. (This is more of an issue with Python's built in exceptions than with something very specific like this `VerificationError` example.)