cannot pass a variable from a function
Steve
dippyd at yahoo.com.au
Thu Jun 17 04:08:33 EDT 2004
Larry Bates wrote:
> Doug,
>
> You are talking about passing by reference. Python
> doesn't do that. It only passes by value, unless you
> pass an object (e.g. list, dictionary, class, etc.).
> In those cases you CAN modify object in the function.
I think this is horribly horribly confused and
confusing and I wish I had never learnt Pascal so that
this reference/value wossname wouldn't contaminate my
thinking. Python doesn't have pointers, thank Offler.
If you google on "pass by reference" and "pass by
value" you will quickly discover that whenever you have
two programmers in a room and ask them to describe
whether a language is one or the other, you will get
three different opinions.
To give an example of why it is so confusing, a pointer
in C has two values, the value of the pointer and the
value of the thing the pointer points to. Let the C
programmers deal with that, we don't have to.
I believe that Tim Peters once declared that Python was
"call by object":
'''I usually say Python does "call by object". Then
people go "hmm, what's that?". If you say "call by
XXX" instead, then the inevitable outcome is a
tedious demonstration that it's not what *they*
mean by XXX. Instead I get to hear impassioned
arguments that "by object" is what any normal person
means by YYY <wink>.'''
Earlier, Doug Jordan wrote:
>>Kirk,
>>Thanks for your input, hoever that is not exactly what I am trying to do.
>>I understand that q is local scope. I was trying to return q and make a
>>call to the function using another variable with global scope.
>>
>>In other language
>>subroutine foo(b,c)
>> c=b*1000
>>return
>>call foo(q,r)
>>where q and r are defines and same type as b,c as function
>>How do I do this in python.
Well, that's sweet, but since I don't read whatever
language this comes from, I don't know what it is
returning. Does it return c? Or perhaps b? A nil
pointer? Some special value indicating no result?
It looks to me like the value of c gets immediately
over-written, so why pass it to the function in the
first place?
The nice thing about Python is that it is explicit
instead of implicit. If you want to return something,
you have to return it.
(The only exception is, if you don't return anything,
you actually return None. *cough*)
def foo(b, c):
# return modified c
c = b*1000
return c
If we go all the way back to your original request, my
understanding was that you wanted to modify the
following to return something:
>>>def prntfctn(y):
>>> for j in y:
>>> print j
But what is it that you are expecting to return? The
last value of y? The first? Everything in y?
def prnt_and_return(y):
# print each item in list y and return the
# entire list with a minus one appended
for item in y:
print item
return y + [-1]
>>> y = [1, 2, 3]
>>> z = prnt_and_return(y)
1
2
3
>>> print y, z
[1, 2, 3], [1, 2, 3, -1]
Regards,
--
Steven.
More information about the Python-list
mailing list