How to return a simple variable from a function (still newbie) ?

Paul Hummer paul at eventuallyanyway.com
Thu Dec 28 10:28:38 EST 2006


How about restructuring your function like this:

def some_function( z,y ):
  z = 2
  y[2] = 'global ?'
  return z

And then you'll have to react to the returning variable, like this in
your code:

x = 5
y = [1,2,3,4]
print x,y
print some_function( x, y )
print x,y

Now, it appears like you want some_function() to change x and y.  It
doesn't really work that way.  You'd have to either pass the variables
by reference into the function, or overwrite the existing values with
whatever the function returns.  That might be beyond your scope now though.

Keep plugging, and welcome to Python!

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>
>   




More information about the Python-list mailing list