Python Programming Ettiquette

zzzzz zzizz_ at notmail.com
Thu Apr 12 01:45:00 EDT 2001


If I want to change a variable value from within a function I believe
I have two options:

a) Directly modify the parameter:
x=[1,2,3]
def add4tolist(inputlist):
   inputlist.append(4)
add4tolist(x)
x
[1,2,3,4]

b) Return the result, keeping the passed parameter untouched:
x=[1,2,3]
def add4tolist(inputlist):
   return (inputlist[:]).append(4)
x=add4tolist(x)
x
[1,2,3,4]

a) is simpler, however, it only works on immutable type
b) is more general, however, you still have to be careful about
accidently modifying mutable parameters.

Is there any accepted programming etiquette on how to best go about
this?

Regards,

zzzzz.
---------------------



More information about the Python-list mailing list