reference or pointer to some object?

Torsten Mohr tmohr at s.netic.de
Thu Jan 13 15:20:06 EST 2005


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.

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?

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

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.

Maybe it is because i did lots of perl programming, but

func(\$s) looks easier to me.

> In my case, rather than your original example, which you want to look
> something like:
> 
>      def func(x):
>          x += 123
> 
>      x = 5
>      func(x)
> 
> I'd just write:
> 
>      x = 5
>      x += 123

You're right, of course.  I'm sorry the second example is still
a bit constructed, but i came across it by writing the hexdump
utility and wanted to reduce overhead.


Best regards,
Torsten.




More information about the Python-list mailing list