In Python, how can I refer to a return-value from a function called within a generator expression?

StackOverflow https://stackoverflow.com/questions/8485003

Simplified, I want to do something like this:

({'publication': obj.pub_name, 'views': obj.views, } for obj = analyze_publication(p) for p in Publication.objects.all())

Of course, that doesn't work.

Right now, I'm using:

({'publication': obj.pub_name, 'views': obj.views, } for obj in (analyze_publication(p) for p in Publication.objects.all()))

I have no idea if the second code piece is how it's done or there's another syntax, or it's not efficient etc. I'm only 2 weeks into Python.

有帮助吗?

解决方案

You have unbalanced parens, but other than that, you should have no (functional) problem nesting generator expressions. As with list comprehensions, they quickly become unwieldy when nested. I'd recommend moving it out to a named generator, for readability's sake.

If you are curious about performance, compare different approaches using the disassembler or profilers.

其他提示

Just another version:

( dict(publication= obj.pub_name, views= obj.views) 
    for obj in map(analyze_publication, Publication.objects.all()) )

Rather than trying to contort the generator expression syntax to allow a name binding, I'd just use a function to produce the binding. Then it is simple:

def pubdict(pub):
    obj = analyze_publication(pub)
    return {"publication": obj.pub_name, "views": obj.views}

itertools.imap(pubdict, Publication.objects.all()
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top