How to return a simple variable from a function (still newbie) ?
Carsten Haese
carsten at uniqsys.com
Thu Dec 28 10:35:50 EST 2006
On Thu, 2006-12-28 at 16:14 +0100, Stef Mientki wrote:
> I want to return a "simple" variable from a function,
> not using the function result.
> Is that in any way possible ??
>
> The code below is from O'Reilly, "Learning Python",
> and there seems no way
> to return a simple var like "z" in the example below.
> Is that true ?
>
> thanks,
> Stef Mientki
>
> <Python>
> def some_function (z, y):
> z = 2
> y[2] = 'global ?'
>
>
> x = 5
> y = [1,2,3,4]
> print x,y
> some_function(x,y)
> print x,y
> </Python>
Please reset your brain and read
http://effbot.org/zone/python-objects.htm , paying particular attention
to the section called "Assignment".
You should also think long and hard about *why* you want to return a
value from a function by modifying an input parameter instead of just
using the return statement. The "return by modifying an input parameter"
approach comes from C where that's the only way to return more than one
value from a function. In Python, no such crutch is necessary or
desirable.
With this in mind, the following quote seems appropriate:
"Trying to write C code using Python isn't going to be fun or
productive." -- Grant Edwards, comp.lang.python, 13 Sep 2006.
Hope this helps,
Carsten.
More information about the Python-list
mailing list