In Pascal we have var
parameters, and functions can change parameter values to new values:
procedure a(var S1, S2: string);
begin
S1:= S1+'test'+S1;
S2:= S1+'('+S2+')';
end;
Does Python have such a feature? Can I change the string
parameter inside the method, or must I use return
and assign the variable later?
Python can return multiple values (in the form of a tuple), obsoleting the need to pass values by reference.
In your simple sample case, even if you were able to apply the same technique, you could not achieve the same result as Python strings are not mutable.
As such, your simple example can be translated to Python as:
def a(s1, s2):
s1 = '{0}test{0}'.format(s1)
s2 = '{}({})'.format(s1, s2)
return s1, s2
foo, bar = a(foo, bar)
The alternative is to pass in mutable objects (dictionaries, lists, etc.) and alter their contents.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With