append to non-existing list
Yves Glodt
y.glodt at sitasoftware.lu
Wed Nov 9 10:07:16 EST 2005
Max M wrote:
> 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 ;-)
Yes me too, I'm gonna stick to that... :-)
More information about the Python-list
mailing list