1

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()
Mohammad Alhashash
  • 1,543
  • 1
  • 14
  • 30
leong
  • 339
  • 3
  • 5
  • 12
  • 1
    Why do you need to use globals at all? –  Aug 04 '12 at 13:44
  • i just try and error bro because im new in python.. but no luck to get it work.. =\ – leong Aug 04 '12 at 13:47
  • 1
    IIRC the official python tutorial doesn't advocate wide usage of globals like this –  Aug 04 '12 at 14:03
  • It's possible that `my_function()` is putting the same element into the list again after `reset_a()` has cleared it. – martineau Aug 04 '12 at 14:08
  • Can you complete the question to be a working example of the problem you're having? At the moment, there's no way to know how the list is getting modified, and `data` isn't defined. – Paul Hankin Aug 04 '12 at 14:17

1 Answers1

0

rest_a() will be executed only if there is a new data not in a

Mohammad Alhashash
  • 1,543
  • 1
  • 14
  • 30
  • yes.. the `#check stuffs` are to check it then take the `data` to look into the list and if not in `a` it will execute.. my objective is sometime like this. – leong Aug 04 '12 at 13:53
  • If you want to get help, post a working example that demonstrates the problem. – Mohammad Alhashash Aug 04 '12 at 17:57