数组没有在python中清空

时间:2016-06-17 16:47:56

标签: python arrays

p:selectOneMenu

这是主要的

from bin import pmGetter
from threading import Thread
import time

possibleRequests = ['test', 'test1']
inbox = []

inbox = pmGetter.getPms()


# time to do threading
def pmGetterLoop():
    while True:
        inbox = pmGetter.getPms()


def inboxReader():
    print('triggered')
    while True:
        tempInbox = []
        tempInbox = inbox.inboxMessage #inboxMesage remains filled?
        print(inbox.inboxMessage)
        i = 0
        while (i < len(tempInbox)):
            if (tempInbox[i] in possibleRequests):
                print('THIS IS WORKING')
            #print(i)
            i+=1
        time.sleep(2)

def commandBreaker(commandString):
    return commandString.split(',')


threadOne = Thread(target=pmGetterLoop)
threadTwo = Thread(target=inboxReader)

threadTwo.start()
threadOne.start()

#print(commandBreaker('this,is,a,test'))#this is what will be used to split commands

由于某种原因我得到了这个输出

import praw
import time


class getPms():
    r = praw.Reddit(user_agent="Test Bot By /u/**********")
    r.login(username='******************', password='*************')

    cache = []
    inboxMessage = []
    file = 'cache.txt'

    def __init__(self):

        self.cache = self.cacheRead(self.file)
        self.bot_run()
        self.cacheSave(self.file)
        time.sleep(2)
        #return self.inboxMessage

    def bot_run(self):
        print(self.inboxMessage, ' why?')
        self.inboxMessage = []
        inbox = self.r.get_inbox(limit=25)

        # print(r.get_friends())#this works
        for message in inbox:
            if message.id not in self.cache:
                #print(message.id)
                print(message.body)
                # print(message.subject)
                self.cache.append(message.id)
                self.inboxMessage.append(message.body)
                # else:
                # print("no messages")

    def cacheSave(self, file):
        with open(file, 'w') as f:
            for s in self.cache:
                f.write(s + '\n')

    def cacheRead(self, file):
        with open(file, 'r') as f:
            cache1 = [line.rstrip('\n') for line in f]
        return cache1
那里的[]为什么?表示阵列已被清空,但是[&#39;测试&#39;这是一个测试&#39;]表示阵列仍然被填充

我尝试通过self.inboxMessage = []

清空数组

1 个答案:

答案 0 :(得分:1)

这是一个问题

inbox = pmGetter.getPms()

def pmGetterLoop():
    while True:
        inbox = pmGetter.getPms()

inbox函数中的pmGetterLoop与函数外部的inbox不同。这些是单独的范围中的单独变量。如果您希望pmGetterLoop能够修改全局inbox变量,则需要告诉它使用全局变量。

def pmGetterLoop():
    global inbox
    while True:
        inbox = pmGetter.getPms()