Re: assignment in a for loop
- From: Ben Finney <bignose+hates-spam@xxxxxxxxxxxxxxx>
- Date: Wed, 17 May 2006 15:11:51 +1000
"MackS" <mackstevenson@xxxxxxxxxxx> writes:
[MackS, please don't top-post.]
Suppose I want to do modify all arguments which are passed to a
function. Do I need to use a list comprehension such as
def f(arg1,arg2,arg3):
arg1,arg2,arg3 = [i+1 for i in (arg1,arg2,arg3)]
...
This would be awful when, eg, one adds an argument to the function
definition. It would require edition of the code at two different
locations.
If you anticipate increasing the number of values passed to the
function, and you're doing the same operation on all of them, why not
pass in a list::
>>> def add_one_to_each(nums):
... """ Makes a new list with each value incremented by one. """
...
... incremented_nums = [x+1 for x in nums]
... return incremented_nums
...
>>> foo = [3, 5, 8]
>>> bar = add_one_to_each(foo)
>>> bar
[4, 6, 9]
--
\ "Some mornings, it's just not worth chewing through the leather |
`\ straps." -- Emo Philips |
_o__) |
Ben Finney
.
- References:
- assignment in a for loop
- From: MackS
- Re: assignment in a for loop
- From: MackS
- assignment in a for loop
- Prev by Date: Re: A Unicode problem -HELP
- Next by Date: Re: python vs perl lines of code
- Previous by thread: Re: assignment in a for loop
- Next by thread: Re: assignment in a for loop
- Index(es):
Relevant Pages
|