2013-04-26 50 views
0

我正在創建一個簡單的python腳本,它使用xmlrpc API檢查WordPress博客上的新評論。我怎樣才能給一個變量在Python中的函數內的另一個值?

我堅持一個循環,應該告訴我,如果有新的評論或沒有。下面的代碼:

def checkComm(): 
    old_commCount = 0; 
    server = xmlrpclib.ServerProxy(server_uri); # connect to WP server 
    comments = server.wp.getComments(blog_id, server_admin, admin_pass, filters); 
    new_commCount = len(comments); 
    if new_commCount > old_commCount: 
     print "there are new comments" 
     old_commCount = new_commCount 
    else: 
     print "no new comments" 

while True: 
    checkComm() 
    time.sleep(60) 

我跳過像blog_id,SERVER_ADMIN等變量。由於他們均沒有產生這個問題。

你能告訴我的代碼有什麼問題嗎?

非常感謝。

+1

你說的「卡住」是什麼意思?你的代碼是怎麼回事? – alexvassel 2013-04-26 12:16:11

+1

你不需要Python中的分號。它們不會損害您的代碼,但它們也不是必需的。 – 2013-04-26 12:17:39

回答

0

你想把它作爲參數傳遞,因爲你每次調用該函數將其復位:

def checkComm(old_commCount): # passed as a parameter 
    server = xmlrpclib.ServerProxy(server_uri) # connect to WP server 
    comments = server.wp.getComments(blog_id, server_admin, admin_pass, filters) 
    new_commCount = len(comments) 
    if new_commCount > old_commCount: 
     print "there are new comments" 
     old_commCount = new_commCount 
     return old_commCount # return it so you can update it 
    else: 
     print "no new comments" 
     return old_commCount 

comm_count = 0 # initialize it here 
while True: 
    comm_count = checkComm(comm_count) # update it every time 
    time.sleep(60) 
+0

我已經解決了您的建議,但沒有使用您的實際代碼。 我不得不將'old_commCount = new_commCount' 'return old_commCount' 以外的if/else語句。如果else語句爲true,那麼它的寫法是old_commCount獲取Null值。 這樣,即使沒有新的評論,它也會隨時更新並返回。 我會盡快發佈完整的代碼(低代表) 再次感謝 – danix 2013-04-26 16:32:39

+0

@danix沒問題。不知道你的意思是空值('None'?)...不明白爲什麼它會這樣做,但很高興幫助:) – 2013-04-26 23:05:34

+0

我再次檢查您的解決方案,現在問題不再出現..對不起,噪音,你的解決方案是完美的工作..再次感謝.. – danix 2013-04-27 11:39:11

相關問題