[英]Python - args an **kargs
我有这个全局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函数的正确语法是什么?
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
您已经正确定义了,没有问题。 您面临的问题是什么?
如果您使用显式参数value
调用函数,则不应使用**kwargs
应使用常规参数:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.