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

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Thu Dec 28 10:28:35 EST 2006


In <d2b4$4593df5a$d443bb3a$12073 at news.speedlinq.nl>, Stef Mientki wrote:

> I want to return a "simple" variable from a function, not using the
> function result.

Why?

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

To return objects the ``return`` statement is used.

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

Add:
    return z

The string content seems to be a question.  No `y` is not global here but
you modify the content of the object that's bound to the local name `y`. 
Modifying an object is different from binding a name to a new object.
``y = ['uno', 'dos', 'tres']`` would not be visible outside the function.

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

Change to:

x = some_function(x, y)

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list