我有全球function
:
def filterBelowThreshold(name, feature, tids, xsongs, **kwargs):
print (name, 'PLAYLIST')
for i, x in enumerate(feature):
if x < value:
track_name = sp.track(tids[i])['name']
xsongs.append(track_name)
print(name, ":", "{} - feature: {}".format(track_name, x))
我想在class
function
内调用它,传递以下参数(其变量在本地声明):
filterBelowThreshold('myname', energy, tids, xsongs, value=0.650)
在class function
内,在函数调用之前,我声明了以下变量:
energy = [item 1, item2, item3, ...]
tids = []
xsongs = []
GLOBAL函数的正确语法是什么?
答案 0 :(得分:0)
<强> test.py 强>
def filterBelowThreshold(name, feature, tids, xsongs, **kwargs):
print kwargs['value']
class Test(object):
def __init__(self):
energy = ['item 1', 'item2', 'item3' ]
tids = []
xsongs = []
filterBelowThreshold('myname', energy, tids, xsongs, value=0.650)
a = Test()
python test.py
将打印0.65
你已经定义正确,没有问题。你面临的问题是什么?
答案 1 :(得分:0)
如果使用显式参数**kwargs
调用函数,则不应使用value
,只需使用普通参数:
def filterBelowThreshold(name, feature, tids, xsongs, value):
print(name, 'PLAYLIST')
for tid, x in zip(tids, feature):
if x < value:
track_name = sp.track(tid)['name']
xsongs.append(track_name)
print("{} : {} - feature: {}".format(name, track_name, x))
并将其称为
filterBelowThreshold('myname', energy, tids, xsongs, value=0.650)
或
filterBelowThreshold('myname', energy, tids, xsongs, 0.650)