Does hashlib support a file mode?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Jul 8 03:46:12 CEST 2011


Andrew Berg wrote:

> On 2011.07.07 08:39 AM, Phlip wrote:
>> On Jul 7, 6:24 am, Andrew Berg <bahamutzero8... at gmail.com> wrote:
>> > On 2011.07.07 08:11 AM, Phlip wrote:> No, I was pointing out that
>> > passing a type is more ... typesafe.
>> >
>> > None is a type.
>>
>> I never said it wasn't.

Unfortunately, it isn't.

None is not a type, it is an instance.

>>> isinstance(None, type)  # is None a type?
False
>>> isinstance(None, type(None))  # is None an instance of None's type?
True

So None is not itself a type, although it *has* a type:

>>> type(None)
<type 'NoneType'>
>>> isinstance(type(None), type)  # is NoneType itself a type?
True


> You are talking about this code, right?
> 
> def file_to_hash(path, m=None):
>     if m is None:
>         m = hashlib.md5()
> 
> What's not a type? The is operator compares types (m's value isn't the
> only thing compared here; even an separate instance of the exact same
> type would make it return False), and m can't be undefined.

The is operator does not compare types, it compares instances for identity.
There is no need for is to ever care about the type of the arguments --
that's just a waste of time, since a fast identity (memory location) test
is sufficient.

This is why I initially thought that Phlip was joking when he suggested
that "m is None" could be type-unsafe. It doesn't matter what type m
has, "m is <anything>" will always be perfectly safe. 




-- 
Steven




More information about the Python-list mailing list