2016-08-12 2 views
0

내가 가진이 function 글로벌 :파이썬 - ** kargs을 인수

나는 (그 변수 로컬 선언) 다음 매개 변수를 전달하는 class 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)) 

:

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

내에서 class function, 함수 호출 이전에 나는 다음 변수를 선언합니다 :

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

tids = []

xsongs = []

전역 함수에 대한 적절한 구문 무엇?

+1

과 같이 호출 코드가 올바르게 보입니다. 나는 그것에 어떤 문제도 실제로 볼 수 없다 ... – refi64

+0

당신이 쓰려고하는 수업을 보여주세요. 현재 시도를 이해할 수 없습니다. ('kwargs' 처리에 관해서는 도움이된다면'value = 0.650'을'filterBelowThreshold'로 넘겨 주면'kwargs'는 하나의 키 'value'를 갖는'dict'가 될 것입니다. '0.650'). – pistache

답변

0

당신은 **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) 
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

인쇄됩니다. 당신이 겪고있는 문제는 무엇입니까? 당신이 명시 적 매개 변수 value로 함수를 호출하는 경우