Last value of yield statement

Amit Khemka khemkaamit at gmail.com
Wed Oct 10 07:02:41 EDT 2007


On 10/10/07, Shriphani <shriphanip at gmail.com> wrote:
> On Oct 10, 3:34 pm, Dustan <DustanGro... at gmail.com> wrote:
> > On Oct 10, 5:19 am, Shriphani <shripha... at gmail.com> wrote:
> >
> > > Hello all,
> >
> > > Let us say I have a function like this:
> >
> > > def efficientFiller(file):
> >
> > Note that you are shadowing the built-in variable 'file' here. Better
> > use 'filename', or something to that effect.
> >
> >
> >
> > >         worthless_list = []
> > >         pot_file = open(file,'r')
> > >         pot_file_text = pot_file.readlines()
> > >         for line in pot_file_text:
> > >                 if line.find("msgid") != -1:
> > >                         message_id = shlex.split(line)[1]
> > >                         if message_id in dictionary:
> > >                                 number = pot_file_text.index(line)
> > >                                 corresponding_crap =
> > > dictionary.get(message_id)
> > >                                 final_string = 'msgstr' + " " + '"' +
> > > corresponding_crap + '"' + '\n'
> > >                                 pot_file_text[number+1] = final_string
> > >                                 yield pot_file_text
> >
> > > efficient_filler =  efficientFiller("libexo-0.3.pot")
> > > new_list = list(efficient_filler)
> > > print new_list
> >
> > > I want to plainly get the last value the yield statement generates.
> > > How can I go about doing this please?
> Well the basic trouble is that the yield statement you see there
> causes it to print the list over and over again when a string
> containing "msgid" is found and the subsequent conditions are
> satisfied. I just want the last list the yield statement generates. I
> don't want the last element of the list generated. just the last
> statement.

I may be being stupid but i really fail to understand that why would
you want to use 'yield' in such a scenario ?

Btw, the following line in your code may cause some unexpected
behavior (in case of duplicates):
<code>
number = pot_file_text.index(line)
</code>

You should rather use 'enumerate' .

Cheers,
-- 
--
Amit Khemka



More information about the Python-list mailing list