Torsten Mohr wrote:
> Hi,
> 
>> Could you give us a more concrete use case?  My suspicion is that
>> anything complicated enough to be passed to a method to be modified will
>> probably be more than a simple int, float, str or tuple...  In which
>> case, it will probably have methods to allow you to update it...
> 
> yes, to be more explicit: I'm quite new to python and i wrote
> a small function that does a hexdump of a string.  That string
> can be quite large, so i suspected a large overhead when the
> string would be copied and handed over to the function.

It isn't.

> But i think my understanding was wrong (though it is not yet
> clear).  If i hand over a large string to a function and the
> function had the possibility to change it, wouldn't that mean
> that it is necessary to hand over a _copy_ of the string?
> Else, how could it be immutable?

You cannot modify a string. Notice that there are no in-place string
methods -- str.strip() for example returns a new string.

> Thinking about all this i came to the idea "How would i write
> a function that changes a string with not much overhead?".

Basically, working with very large strings is costly (it saves overhead
otherwise). So do make smaller parts and operate on these chunks.

> def func(s):
>   change s in some way, remove all newlines, replace some
>     charaters by others, ...
>   return s
> 
> s = func(s)
> 
> This seems to be a way to go, but it becomes messy if i hand over
> lots of parameters and expect some more return functions.

You can use tuples for that. Automatic tuple packing helps:

def func(x, y):
    # change x, y and generate z

    return x, y, z

x, y, z = func(x, y)

> Maybe it is because i did lots of perl programming, but
> 
> func(\$s) looks easier to me.

It does, but in fact the problem is not that the string is passed by
value, but that the string is not modifiable...

Reinhold
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to