非emptylist的Python函数返回None [英] Python function returns None for non emptylist

查看:116
本文介绍了非emptylist的Python函数返回None的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一个递归函数来查询Racktables数据库,并跟踪对象之间的连接并将它们放入一个列表中。



所有结果都被附加和扩展我列出一个列表作为论点。
函数在 return 语句中运行,但列表返回后 None



我在函数中添加了一些 print 语句进行调试,列表中包含了我需要的数据:

  def plink(Objid,PortId,mylist):

if len(mylist)< 2:#如果这是第一次运行,他在列表中的初始数据
mylist.append(Objid)
mylist.append(PortId)

res = rt.GetLink PortId)#check if port is connected

if res ==(False,False):
print'因为没有连接而退出'#debug
return mylist #If not connected return列表

nextObj = rt.GetPortObjid(res [1])$ ​​b $ b mylist.extend(res)
mylist.append(nextObj)

ispatch = rt.CheckObjType(nextObj,50080)
如果ispatch [0] [0] == 0:#如果连接到非补丁单元,则将数据添加到列表并退出
print退出,因为next对象{0}不是patchunit,mylist是{1}。format(nextObj,mylist)#debug
return mylist

patchPorts = rt.GetAllPorts(nextObj)

如果len(patchPorts)!= 2:#检查patchunit是否有r ight端口数
mylist.append(Error,patch-unit must have two ports)
return mylist

如果patchPorts [0] [2] == res [1]:#检查哪个端口是不可见的并再次调用该函数
print mylist
plink(nextObj,patchPorts [1] [2],mylist)
else:
print mylist
plink(nextObj,patchPorts [0] [2],mylist)

results = ['Initial data']
allconn = plink(159,947,results)
print完整的连接是{0}。格式(allconn)



运行这段代码给出:

  ['初始数据',159,947,'C150303-056',4882,1591L] 
['初始数据',159,947,'C150303-056',4882,1591L,'C140917-056',因为下一个对象1114不是分组单元,mylist是['Initial data',159,947,'C150303-056',4882,1591L,'C140917- 056',4689,727L,'C140908-001',3842,1114L]
完整连接是

调试打印显示的列表完全按照它应有的填充,但是当在函数外部打印时,也会在事先分配给变量之后得到None。



我正在写这个在python 2.6上运行在CentOS 6服务器上。我可以在virtualenv中运行它,作为最后的手段,但如果可能的话,我会避免它。

$ c>如果patchPorts [0] [2] == res [1]:#check哪个端口是不可见的并再次调用该函数
print mylist
plink(nextObj,patchPorts [1] [2] ,mylist)
else:
print mylist
plink(nextObj,patchPorts [0] [2],mylist)

递归调用函数不会自动使内部调用将返回值传递给外部调用。您仍然需要明确 return

  if patchPorts [0] [ 2] == res [1]:#check哪个端口是不可见的并再次调用该函数
print mylist
return plink(nextObj,patchPorts [1] [2],mylist)
else :
print mylist
return plink(nextObj,patchPorts [0] [2],mylist)


I have written a recursive function to make queries to Racktables database and follow the connections between objects and put them in a list.

All the results get appended and extended to a list that I give as argument. The function works up to the return statement, but the list gives None after being returned

I have added some print statements in the function to debug, and the list contains the data I need up to that point:

def plink(Objid, PortId, mylist):

    if len(mylist) < 2:                         #If this is the first run put he initial data in the list
        mylist.append(Objid)
        mylist.append(PortId)

    res = rt.GetLink(PortId)                    #check if port is connected 

    if res == (False, False):
        print 'exiting because not connected'   #debug
        return mylist                           #If not connected return list

    nextObj = rt.GetPortObjid(res[1])
    mylist.extend(res)
    mylist.append(nextObj)

    ispatch = rt.CheckObjType(nextObj, 50080)
    if ispatch[0][0] == 0:                      #If connected to a non-patch-unit, add data to list and exit
        print "exiting because next object {0} is not a patchunit, mylist is {1}".format(nextObj, mylist)  #debug
        return mylist

    patchPorts = rt.GetAllPorts(nextObj)

    if len(patchPorts) != 2:                    #check if the patchunit has the right number of ports
        mylist.append("Error, patch-unit must have exactly two ports")
        return mylist

    if patchPorts[0][2] == res[1]:              #check which port is unseen and call the function again
        print mylist
        plink(nextObj, patchPorts[1][2], mylist)
    else:
        print mylist
        plink(nextObj, patchPorts[0][2], mylist)

results = ['Initial data']
allconn = plink(159, 947, results)
print "The full connection is {0}".format(allconn)

(I have skipped the DB constructs here)

Running this code gives:

['Initial data', 159, 947, 'C150303-056', 4882, 1591L]
['Initial data', 159, 947, 'C150303-056', 4882, 1591L, 'C140917-056', 4689, 727L]
exiting because next object 1114 is not a patchunit, mylist is ['Initial data', 159, 947, 'C150303-056', 4882, 1591L, 'C140917-056', 4689, 727L, 'C140908-001', 3842, 1114L]
The full connection is None

The debug prints show the list being populated exactly as I it is supposed to, but then I get None when printing outside the function, also after assigning to variable beforehand.

I am writing this to run it on a CentOS 6 server with python 2.6. I could run it in virtualenv, as a last resort, but I would avoid it if possible

解决方案

if patchPorts[0][2] == res[1]:              #check which port is unseen and call the function again
    print mylist
    plink(nextObj, patchPorts[1][2], mylist)
else:
    print mylist
    plink(nextObj, patchPorts[0][2], mylist)

Calling a function recursively does not automatically make the inner call pass the return value up to the outer call. You still need to explicitly return.

if patchPorts[0][2] == res[1]:              #check which port is unseen and call the function again
    print mylist
    return plink(nextObj, patchPorts[1][2], mylist)
else:
    print mylist
    return plink(nextObj, patchPorts[0][2], mylist)

这篇关于非emptylist的Python函数返回None的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆