Python骑着一辆**卡格斯

2024-04-26 17:51:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个全局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))

我想在classfunction中调用它,传递以下参数(其变量在本地声明):

^{pr2}$

class function中,在函数调用之前,我声明以下变量:

energy = [item 1, item2, item3, ...]

tids = []

xsongs = []

全局函数的正确语法是什么?在


Tags: namein声明fordeffunctiontrack全局
2条回答

测试.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

你已经定义好了,没问题。你面临的问题是什么?在

不应使用**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))

把它叫做

^{pr2}$

或者

filterBelowThreshold('myname', energy, tids, xsongs, 0.650)

相关问题 更多 >