一个小问题
Topic source你要去看python的文档:
b64decode(s, altchars=None, validate=False)
Decode the Base64 encoded bytes-like object or ASCII string s.
b64decode的第一个参数是bytes或者str
- 1
你要去看python的文档:
b64decode(s, altchars=None, validate=False)
Decode the Base64 encoded bytes-like object or ASCII string s.
b64decode的第一个参数是bytes或者str
cc冰山蝶雨
交互式界面执行如下代码:
>>> assert 1 == 1, print(1)
>>>
并没有打印出1,即没有执行print(1)语句,所以上面的测试代码的格式我有点没看明白,相当于测试时如果前面的等式成立,逗号后面的那两个语句都没有执行。
如果四个函数调用都要执行的话,如下答案就会报错,测试如下:
>>> import base64
>>> def safe_base64_decode(s):
length = len(s)
mod = length % 4
s += b'='*(4-mod) if mod else b''
return base64.b64decode(s)
>>> safe_base64_decode('YWJjZA==')
Traceback (most recent call last):
File "<pyshell#27>", line 1, in <module>
safe_base64_decode('YWJjZA==')
File "<pyshell#26>", line 4, in safe_base64_decode
s += b'='*(4-mod) if mod else b''
TypeError: can only concatenate str (not "bytes") to str
>>>
所以可能应该分两种情况考虑:传入参数的类型是str还是bytes