如何在python中的函数内给变量另一个值?

时间:2013-04-26 12:10:03

标签: python wordpress loops if-statement xml-rpc

我正在创建一个简单的python脚本,使用xmlrpc API检查WordPress博客上的新评论。

我遇到了一个应该告诉我是否有新评论的循环。这是代码:

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 个答案:

答案 0 :(得分: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)