Python:如何将NonType变量转换为String?

AME*_*AME 1 python

我有以下功能:

>>> def rule(x):
...     rule = bin(x)[2:].zfill(8)
...     rule = str(rule)
...     print rule
Run Code Online (Sandbox Code Playgroud)

我试图将规则转换为字符串,但是当我运行以下命令时,我得到的是:

>>> type(rule(20))
00010100
<type 'NoneType'>
Run Code Online (Sandbox Code Playgroud)

什么是NoneType变量,如何将此变量转换为字符串?

谢谢,

Sil*_*ost 6

你需要做的:

def rule(x):
    return bin(x)[2:].zfill(8)
Run Code Online (Sandbox Code Playgroud)

None如果return在执行函数期间没有遇到任何语句,则所有函数都会隐式返回(就像在您的情况下一样).