jeudi 7 juillet 2016

How to declare python global list


I'm having some problems declaring a list. It seems like I cannot delete all the elements in the list. At the reset_a printout, it display an empty list but after another around of checking the list in myFunction, the previous element is still in the list. How can I declare a global list and use it all over others defined? a = [] def reset_a(): global a del a[:] print a def myFunction(): global a #check other stuffs........... then.. print a if data not in a: a.append(data) time.sleep(5) reset_a() if __name__=='__main__': while True: myFunction() Edited: I found a way to get it done. global a a = [] def reset_a(): del a[:] print a def myFunction(): #check other stuffs...........and get 'data' then.. print a if data not in a: a.append(data) time.sleep(5) reset_a() if __name__=='__main__': while True: myFunction()

Aucun commentaire:

Enregistrer un commentaire