append to non-existing list

Max M maxm at mxm.dk
Wed Nov 9 09:40:29 EST 2005


Yves Glodt wrote:
> bruno at modulix wrote:
> 
>> Yves Glodt wrote:
>>
>>> Hello,
>>>
>>> if I do this:
>>>
>>> for row in sqlsth:
>>> ________pkcolumns.append(row[0].strip())
>>> ________etc
>>>
>>>
>>> without a prior:
>>>
>>> pkcolumns = [];
>>>
>>>
>>> I get this error on first iteration:
>>> UnboundLocalError: local variable 'pkcolums' referenced before 
>>> assignment
>>>
>>>
>>> I guess that's normal as it's the way python works...?!?


Well you could do something like this. (Untested and unrecommended)

     self.__dict__.setdefault('pkcolumns', []).append(row[0].strip())

Personally I find

     pkcolumns = []
     pkcolumns .append(row[0].strip())

to be nicer ;-)

-- 

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science



More information about the Python-list mailing list