tags:

views:

77

answers:

2

I'm using yield to process each element of a list. However, if the tuple only has a single string element, yield returns the characters of the string, instead of the whole string:

self.commands = ("command1")
...
for command in self.commands:
        yield command            # returns 'c' not 'command1'

how can i fix this?

Thanks

+3  A: 

A tuple having only 1 element should be written with a trailing comma.

self.commands = ("command1",)
KennyTM
oh yeah, i remember reading that now. thanks.
timmy
...With the parentheses being optional.
Beau Martínez
@timmy: It is a good idea to accept the answer if it helped you. Go ahead and click on the "tick mark" icon next to the answer.
Manoj Govindan
yes its duty as a stackoverflow community member :)
Tumbleweed
@manoj, shahjapan - would be great if you guys had a little patience. i can't accept an answer immediately
timmy
@timmy: my apologies.
Manoj Govindan
A: 
self.commands = ["command1"]

You never told the loop that you had a list, so it's treating the string as the sequence.

edit: or you could just fix the tuple, as recommended ... I assumed you'd want to use a list instead of a tuple.

MikeRand