python md5,d.update(strParam).hexdigest()返回NoneType.=,为什么?

YSY*_*YSY 0 python md5

>>> d = md5.new()
>>> d.update('a').hexdigest()
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'hexdigest'
Run Code Online (Sandbox Code Playgroud)

这会工作 -

>>> d = md5.new()
>>> d.update('a')
>>> d.hexdigest()
'0cc175b9c0f1b6a831c399e269772661'
Run Code Online (Sandbox Code Playgroud)

是否有缩短python代码的解释?

ber*_*nie 6

你可以这样做:

md5.new('a').hexdigest()
Run Code Online (Sandbox Code Playgroud)

文档中解释:

new(arg)返回一个新的md5对象.如果arg 存在,则进行方法调用 update(arg).


但是md5被弃用了.
hashlib改用.

编辑:
还有一些问题,md5根据您的目的,您可能希望使用更安全的哈希函数,例如SHA-256:

import hashlib
hashlib.sha256('a').hexdigest()
Run Code Online (Sandbox Code Playgroud)

请注意,SHA-256将需要更长的时间来计算,因此如果您有时间限制,这可能不是最佳选择.