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!

7
  • How is what you want different from what you've already written? I have the feeling I'm missing something. Commented Apr 2, 2012 at 3: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? Commented Apr 2, 2012 at 3: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 ... Commented Apr 2, 2012 at 3:12
  • Tuples are immutable, you need use Lists Commented Apr 2, 2012 at 3:27
  • or else if are particular about tuples, you need to use list of tuples to do that. Not tuples of tuples. Commented Apr 2, 2012 at 3:29

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I guess I meant a list. But I just tried it as an array as well and it didn't work either :/
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.