list comprehension

John Machin sjmachin at lexicon.net
Sat Jul 1 18:02:19 EDT 2006


On 2/07/2006 6:43 AM, a wrote:
> if i remove the global
> i get an undefined error!

That's because you have feed_list in one place and feeds_list in 
another. Ditto feed_id and feeds_id. Your code was *already* broken 
before you removed the global(s).

> it was suggested as a solution in this group below

Bruno wrote, in the thread that you quote: "Globals are *evil*. And 
functions modifying globals is the worst possible thing." Is that what 
you call "suggested as a solution"?

> 
> http://groups.google.com/group/comp.lang.python/browse_thread/thread/b7b2dcdc3e109f3e?hide_quotes=no#msg_81305eeee43f82c6
> thanks
> Simon Forman wrote:
>> a wrote:
>>> hi simon thanks for your reply
>> You're most welcome
>>
>>
>>> what if i want to do this
>>> 	        feed_list=[]
>>> 		feed_id=[]
>>> 		for ix in feeds_list_select:
>>> 			global feeds_list
>>> 			global feeds_id
>>> 			feeds_list.append(ix.url)
>>> 		       feeds_id.append(ix.id)
>>>
>>> ie not one variable but more variables
>>> thanks
>> in a case like this I would usually reach for the zip() function, with
>> the "varargs" * calling pattern
>>
>> N = [(ix.url, ix.id) for ix in feeds_list_select]
>>
>> feed_list, feed_id = zip(*N)
>>
>>
>> or just
>>
>> feed_list, feed_id = zip(*[(ix.url, ix.id) for ix in
>> feeds_list_select])
>>
>>
>> btw, please note that the global statements in your example are
>> unnecessary..  *totally* unnecessary.  :-D
> 



More information about the Python-list mailing list