Does hashlib support a file mode?

Phlip phlip2005 at gmail.com
Fri Jul 8 05:26:56 CEST 2011


> I worded that poorly. None is (AFAIK) the only instance of NoneType, but
> I should've clarified the difference.> The is operator does not compare types, it compares instances for identity.

None is typesafe, because it's strongly typed.

However, what's even MORE X-safe (for various values of X) is a method
that takes LESS for its arguments. That's why I switched from passing
an object to passing a type, because the more restrictive argument
type is more typesafe.

However, the MOST X-safe version so far simply passes a string, and
uses hashlib the way it designs to be used:

def file_to_hash(path, hash_type):

    hash = hashlib.new(hash_type)

    with open(path, 'rb') as f:

        while True:
            s = f.read(8192)
            if not s:  break
            hash.update(s)

    return hash.hexdigest()



More information about the Python-list mailing list