2

I have a function:

def save(self, text, *index): 
    file.write(text + '\nResults:\n')
    if index == (): index = (range(len(self.drinkList)))
    for x in index:
        for y in self.drinkList[x].ing:
            file.write('min: ' + str(y.min) + ' max: ' + str(y.max) + ' value: ' + str(y.perc) + '\n')
        file.write('\n\n')
    file.write('\nPopulation fitness: ' + str(self.calculatePopulationFitness()) + '\n\n----------------------------------------------\n\n')

Now, when I pass one argument as an index the function works as it is supposed to, but when I pass a tuple of 2 indices I get an TypeError: list indices must be integers, not tuple. What should I change?

2
  • 2
    Can you show how you're calling that function? Commented Nov 8, 2011 at 19:44
  • self.save('Resulted in ' , (index1, index2)) self.save('Resulted in ' , index) The first call gives an error, when I pass it without making it a tuple the resulting file is empty. Commented Nov 8, 2011 at 19:46

4 Answers 4

5

The save(self, text, *index) syntax means that index is itself a tuple with all the arguments passed to save after the text one.

So, for instance, if you have in your code:

myobject.save("sample text", 1, 2, 3)

then index will be the tuple (1, 2, 3) and the for x in index will correctly loop over values 1, 2, 3.

On the other hand, if you haveL

myobject.save("sample text", (1,2))

then index will be the 1-element tuple ((1,2),) and the x in the loop will get the value (1,2), hence the TypeError.

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

Comments

3

It depends on what parameters you were actually trying to pass. I presume you called something to the effect of:

object.save("hello world", (3, 4, 5))

When you use the * operator, you do not need to pass the variable number of arguments as a tuple. Instead, everything that you pass after the fixed arguments is wrapped into a list. Therefore, in this case, the variable index refers to [(3, 4, 5)], not [3, 4, 5].

You should call the function save like this instead:

object.save("hello world", 3, 4, 5)

The variable index now refers to [3, 4, 5].

If, for some reason, you wanted to still pass a tuple, simply change your function definition to:

def save(self, text, index): # Observe the lack of '*'

1 Comment

Note that if you wish to continue to allow passing just one argument, this alternate could be something like def save(self, text, index=()).
1

With the *index definition, you have to call the function as save(self, text, index1, index2), and index will be a tuple, (index1, index2). If you are passing a tuple to save after the argument, text, you can leave the * out.

Comments

0

Instead of calling your method with a tuple of indices like

x.save("ababs",(0,1))

Just call it with the indices one after another as if they were distinct paramaters to the method

x.save("ababs",0,1)

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.