【发布时间】:2020-01-07 11:37:50
【问题描述】:
我正在尝试将 classmethod 的返回值分配给 init 方法变量,但显示错误“TypeError: cannot unpack non-iterable NoneType object”
我尝试分配给一个变量,可以看到它返回“元组”
class port_mappings:
ip_prefix=[]
def __init__(self,node1_id,node2_id):
self.node1_id=node1_id
self.node2_id=node2_id
self.node1_ip,self.node2_ip= port_mappings.gen_ip_prefix()
@classmethod
def gen_ip_prefix(cls):
if len(cls.ip_prefix)==0:
cls.ip_prefix.append("10.0.0.0/31")
print(cls.ip_prefix)
return "10.0.0.0/31","10.0.0.1/31"
else:
pass
node_list=port_mappings("95","96")
由于它正在返回元组,所以我正在尝试以下操作,但是
self.node1_ip,self.node2_ip= port_mappings.gen_ip_prefix()
它显示以下错误
TypeError: 无法解压不可迭代的 NoneType 对象
【问题讨论】:
-
如果
len(cls.ip_prefix) != 0,那么你的函数进入else: pass并返回None。 -
嗨 primusa,我还有一些其他代码要放入 else 块中。首先我需要从返回方法中为这个实例变量赋值
-
感谢 Primusa,第二次初始化它遇到了 else 块。我现在用 else 块中的一些代码修复了它。谢谢
标签: python-3.x