Python return values

koranthala at gmail.com koranthala at gmail.com
Mon Jan 5 17:59:35 CET 2009


I have a newbie doubt about Python return values.

In (say) C/C++, if we try to return a value which is stored inside the
procedure stack, we will get an error when trying to access it outside
of that procedure.
For example:
function foo():
   dcl y int
   dcl x pointer to int pointing to y
   return x


function bar():
   x = foo()
   ...
   use x

This will error out since the memory has be taken back.

Now, in Python, we do it everytime, because all variables are
references, and even returns just copies the references.
function pyfoo():
  return 786

function pyfoo1():
  x = xclass()
  return x

function pybar():
  x = pyfoo()
  y = pyfoo1()
  ...
  use x, y

Why doesnt it error out?



More information about the Python-list mailing list