what does 'a=b=c=[]' do

Ethan Furman ethan at stoneleaf.us
Thu Dec 22 08:20:10 EST 2011


Rolf Camps wrote:
> alex23 schreef op wo 21-12-2011 om 16:50 [-0800]:
>> I'd say that _is_ the most pythonic way, it's very obvious in its
>> intent (or would be with appropriate names). If it bothers you that
>> much:
>>
>>     def listgen(count, default=[]):
>>         for _ in xrange(count):
>>             yield default[:]
>>
>>     x, y, z = listgen(3)
>>
> I would change your function to (Python3.x):
> 
> 	def empty_lists(count):
> 	    for _ in range(count):
> 		yield []

While it's good to be careful, default mutable arguments have their 
place.  Alex's versioun allows one to use an already existing list and 
get shallow copies of it, yours will only create empty lists.

a, b, c = listgen([1, 2, 3])
# a, b, & c are bound to different lists

~Ethan~



More information about the Python-list mailing list