2

I am coding in python and ....

I have a quick question. I am trying to reset the values of a global array by calling a certain function but am having difficulty. Here is my code at the moment:

CHOICES = (('1', 'First'), ('2', 'Second'))

def set_choices():
    global CHOICES
    CHOICES = (('3', 'Third'), ('4', 'Fourth'))

Essentially what I want to do is reset the array CHOICES by calling the function from some other function. Is there a way to do that?

Thanks!

Mars J
  • 902
  • 3
  • 15
  • 23
  • How is what you want different from what you've already written? I have the feeling I'm missing something. – Cameron Apr 02 '12 at 03:02
  • Did you actually call your function `set_choices()` in your program? Or there are some other information missing? And is your CHOICES supposed to be a list or a set? – George Apr 02 '12 at 03:08
  • Yeah I did call set_choices() in my original program. and yes it is supposed to a be a set. What I am doing is calling set_choices() in one function. Then in another file I am importing the array CHOICES. When I call set_choices in the first function and then import the array in the next the change doesn't really transfer ... – Mars J Apr 02 '12 at 03:12
  • Tuples are immutable, you need use Lists – Surya Apr 02 '12 at 03:27
  • or else if are particular about tuples, you need to use list of tuples to do that. Not tuples of tuples. – Surya Apr 02 '12 at 03:29
  • The code you've posted looks pretty good; If you're having a problem it's probably not here, can you show us some more code, enough to see where you're having the trouble? – SingleNegationElimination Apr 02 '12 at 03:34
  • I ran it again, your code seems to be doing the exact thing he wants to do. I suspect the error is somewhere else (in the other part of your code). Maybe post some more code and let us take a look where the problem is? – George Apr 02 '12 at 03:39

2 Answers2

4
myObject = [('1', 'First'), ('2', 'Second')] 
CHOICES = set(myObject)

def set_choices():
    global CHOICES
    CHOICES.clear() # Remove the element from set CHOICES
    # Do some of your changes here
    anotherObject = [('3', 'Third'), ('4', 'Fourth')]
    CHOICES[:] = set(anotherObject)


print(CHOICES) # Before calling set_choices
set_choices()
print(CHOICES) # After you calling set_choices

I think this will work. But I don't know if using set and tuple is a good idea, I personally would suggestion you to use list of list instead. Are there particular reason to use a set instead of other options?

Output:

{('2', 'Second'), ('1', 'First')}
{('4', 'Fourth'), ('3', 'Third')}

Respond to your comment to use list:

CHOICES = [['1', 'First'], ['2', 'Second']]

def set_choices():
    # Changed since the comment of another member aaronasterling
    # Removed the use of global
    CHOICES[:] = [['3', 'Third'], ['4', 'Fourth']]

print(CHOICES)
set_choices()
print(CHOICES)

Output:

[['1', 'First'], ['2', 'Second']]
[['3', 'Third'], ['4', 'Fourth']]

To learn more about slice assignment, check out this SO question & answer.

Community
  • 1
  • 1
George
  • 4,514
  • 17
  • 54
  • 81
  • I guess I meant a list. But I just tried it as an array as well and it didn't work either :/ – Mars J Apr 02 '12 at 03:23
1

If you want to do this with a list then there's no need for the global keyword.

CHOICES = [('1', 'First'), ('2', 'Second')

def set_choices():
    CHOICES[:] = (('3', 'Third'), ('4', 'Fourth'))

This will replace the content of the list without changing the reference. It works by slice assignment. The CHOICES[:] references a slice of the whole list.

aaronasterling
  • 68,820
  • 20
  • 127
  • 125