[Discuss] A question for the Python gurus
Adam Parkin
pzelnip at telus.net
Fri Jul 28 22:40:19 PDT 2006
Combined replies.....
Deryk Barker wrote:
>> It's also my understanding that as a result of this any modifications
>> to an argument passed into a function will be seen outside of the
>> function.
>
> No - because assignment in python is also by reference:
Actually that was what I meant, I just worded my sentance poorly. I
meant "any modifications to an argument in the form of method calls to
modify internal data members will be seen outside of the function". Ie:
def foo(someObject):
someObject.setDataMember(20)
someObject.setDataMember(10)
foo(someObject)
print someObject.getDataMember() # prints 20
So thanks for the long explanation, but it was stuff I already knew. =8->
> In python it can't.
>
> However, you can easily write:
>
> x,y = y, x
You mean in the calling function rather than in some called function
right? Like this would not work:
def swap (x, y):
x,y = y,x
x = 5
y = 10
swap (x,y)
print str(x) + " " + str(y) # will still print "5 10", not "10 5"
> It's correct, but not because of the way arguments are passed. Arrays
> are mutable and if you pass an array to a function the array contents
> can be changed:
Right, but again, I knew that. =8-p
> this does not work for strings, because - as you rightly say, strings
> are immutable.
Well, the actual problem I was working on that lead me to ask was
reading from stdin. I just think this code looks wrong/ugly to me:
while (1):
line = sys.stdin.readline()
if (not line):
break
# now do something with line
I'd rather have something that looked like:
while (line = sys.stdin.readline())
# do something with line
but of course this isn't valid (assignment statements aren't
expressions, or so it would seem). So my thinking was to write a
function that would return true/false depending on whether or not EOF
was reached, and to modify its only argument with whatever was read from
stdin. Something like (of course this doesn't work, because the
assignment isn't seen outside of the function):
def readLineFromSTDIN (s):
s = sys.stdin.readline()
if (s):
return 1
else:
return 0
while (readLineFromSTDIN(line)):
# do something with line
But of course, as mentioned this doesn't work. An assignment isn't an
expression in Python, and you can't modify arguments to functions, so
you're stuck with the infinite loop with a if/break inside it (which
again, I think is ugly).
> Actually, if all you want to do is remove the trailing newline, why not
> simply:
>
> string = string[:-1]
>
> to slice it off?
As pointed out by Andrew Resch, this is problematic if you don't know if
the string ends with \n or not. The above code is equivalent (roughly)
to Perl's *chop* function (the only difference is that in Perl chop and
chomp don't return a string but rather a value that IIRC indicates how
many characters were removed).
> (Hmm, will this work in windoze? well, if not why not simply use the
> rstrip assignment instead of creating a function whose only purpose is
> to call rstrip?)
Yes, works in Windows (well, at least under Cygwin). And it's not that
I want to write chomp in Python, it was just a simple example of what I
was trying to achieve (modifying a string passed in to a function).
One thing I don't understand though, why are strings in Python
immutable? That seems to be a fairly significant limitation from an
efficiency standpoint as it means any kind of string processing involves
creating new strings repeatedly instead of just modifying in place.
Though maybe it's just my C-ish "strings are just arrays of characters"
way of looking at things. =;->
Thanks for the replies.... =8->
--
Adam Parkin
E-mail: pzelnip at telus.net
----------------------
The unexamined life is not worth living
-- Socrates
More information about the Discuss
mailing list