Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How do I pass a string by reference?

Tags:

python

string

From this link: How do I pass a variable by reference?, we know, Python will copy a string (an immutable type variable) when it is passed to a function as a parameter, but I think it will waste memory if the string is huge. In many cases, we need to use functions to wrap some operations for strings, so I want to know how to do it more effective?

like image 307
halostack Avatar asked Nov 28 '12 15:11

halostack


People also ask

How do you pass a string by passing by reference?

First, we have the function definition “DisplayString,” where a constant string reference is passed. The constant strings are defined and initialized in the main function as “str1” and “str2”. After that, pass these constant strings to the function “InputString”.

Does Python pass strings by value or reference?

Python utilizes a system, which is known as “Call by Object Reference” or “Call by assignment”. In the event that you pass arguments like whole numbers, strings or tuples to a function, the passing is like call-by-value because you can not change the value of the immutable objects being passed to the function.

Is it possible to pass by reference in Python?

Python passes arguments neither by reference nor by value, but by assignment.

How do you pass a string to an object in Python?

If you want to add 2 ways to initialize/create the object, you can use the *args parameter. I would suggest you try parsing the string outside the creation of the object and pass the values in after. As it stands now, the only way to create the object is by passing in a long string.


2 Answers

Python does not make copies of objects (this includes strings) passed to functions:

>>> def foo(s): ...     return id(s) ... >>> x = 'blah' >>> id(x) == foo(x) True 

If you need to "modify" a string in a function, return the new string and assign it back to the original name:

>>> def bar(s): ...     return s + '!' ... >>> x = 'blah' >>> x = bar(x) >>> x 'blah!' 

Unfortunately, this can be very inefficient when making small changes to large strings because the large string gets copied. The pythonic way of dealing with this is to hold strings in an list and join them together once you have all the pieces.

like image 67
Steven Rumbalski Avatar answered Sep 19 '22 23:09

Steven Rumbalski


Python does pass a string by reference. Notice that two strings with the same content are considered identical:

a = 'hello' b = 'hello' a is b        # True 

Since when b is assigned by a value, and the value already exists in memory, it uses the same reference of the string. Notice another fact, that if the string was dynamically created, meaning being created with string operations (i.e concatenation), the new variable will reference a new instance of the same string:

c = 'hello' d = 'he' d += 'llo' c is d        # False 

That being said, creating a new string will allocate a new string in memory and returning a reference for the new string, but using a currently created string will reuse the same string instance. Therefore, passing a string as a function parameter will pass it by reference, or in other words, will pass the address in memory of the string.

And now to the point you were looking for- if you change the string inside the function, the string outside of the function will remain the same, and that stems from string immutability. Changing a string means allocating a new string in memory.

a = 'a' b = a    # b will hold a reference to string a a += 'a' a is b   # False 

Bottom line:

You cannot really change a string. The same as for maybe every other programming language (but don't quote me). When you pass the string as an argument, you pass a reference. When you change it's value, you change the variable to point to another place in memory. But when you change a variable's reference, other variables that points to the same address will naturally keep the old value (reference) they held. Wish the explanation was clear enough

like image 44
Shay Yzhakov Avatar answered Sep 21 '22 23:09

Shay Yzhakov