modifying a list element from a function

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu Apr 2 05:07:43 EDT 2009


En Mon, 30 Mar 2009 13:19:57 -0300, TP <Tribulations at paralleles.invalid>  
escribió:
> Adrian Dziubek wrote:
>
>> Could you explain your high level goal for this? It looks like a very
>> wicked way of doing things. Have You tried to read the list methods'
>> documentation? Maybe there you find something you need (like
>> list.index)?
>
> I have a "disambiguation" function that modifies a text
> contained "somewhere" in each element of a list. The way this text is
> accessed by the function should depend on an external accessor, or  
> setter,
> so that any list could be processed, in a generic way.

Use a "setter" function (as oposed to a "getter" that you used in your  
example). A direct translation woud be:

def set_elem_1(elem, value):
     elem[1] = value

def assign(setter, list_elem, new_textvalue):
     setter(list_elem, new_textvalue)

assign(set_elem_1, a[0], "co")

I imagine that you process the whole list:

for elem in some_list:
   set_elem_1(elem, new_value)

-- 
Gabriel Genellina




More information about the Python-list mailing list