generator function - Called and accepts XML attributes- Python 3.5

Chris Angelico rosuav at gmail.com
Sat Sep 10 20:58:09 EDT 2016


On Sun, Sep 11, 2016 at 10:46 AM, Sayth Renshaw <flebber.crue at gmail.com> wrote:
> Hi
>
> I want to create a generator function that supplies my calling function a file, how though do I get the generator to accept attributes in the argument when called?
>
> This works not as a generator

The way I'm reading your code, it's not the generator that's the
difference here. Consider these lines:

>         for item in doc['meeting']['race']:
>
> def return_files(file_list):
>     for filename in sorted(file_list, *attribs):
>             for item in doc([attribs]):

Firstly, did you mean for *attribs to be part of the signature of
return_files? As it is, it's being given to sorted(), which won't work
(the other args to sorted are keyword-only).

Assuming that to be the case, what you're trying to do is subscript an
object with a variable set of attributes:

return_files(files, "meeting", "race')

That can best be done with a little loop:

def return_files(file_list, *attribs):
    ...
    for attr in attribs:
        doc = doc[attr]
    for item in doc:

If that's not what you want, can you further clarify the question?

ChrisA



More information about the Python-list mailing list