Functions continuing to ru after returning something?

B

Bradley Hintze

I may be having a brain fart, but is it at all possible to have a
function first return a value then continue its calculation. Like this
simple example:

my_var = 5
def my_function():
return my_var
my_var +=1

This obviously won't work as written but is there a cleaver way around this.

--
Bradley J. Hintze
Graduate Student
Duke University
School of Medicine
801-712-8799
 
A

Aidan

Bradley said:
I may be having a brain fart, but is it at all possible to have a
function first return a value then continue its calculation. Like this
simple example:

my_var = 5
def my_function():
return my_var
my_var +=1

This obviously won't work as written but is there a cleaver way around this.

def my_function():
my_var = 5
while my_var <= 10:
yield my_var
my_var += 1
.... print x
5
6
7
8
9
10
 
M

Maxwell Hansen

I may be having a brain fart, but is it at all possible to have a
function first return a value then continue its calculation. Like this
simple example:

my_var = 5
def my_function():
return my_var
my_var +=1

This obviously won't work as written but is there a cleaver way around this.

return stops execution of any code remaining in the function, but just
out of curiosity, why do you need to do this?
 
P

Peter Otten

Bradley said:
I may be having a brain fart, but is it at all possible to have a
function first return a value then continue its calculation. Like this
simple example:

my_var = 5
def my_function():
return my_var
my_var +=1

This obviously won't work as written but is there a cleaver way around
this.
.... global n
.... try:
.... return n
.... finally:
.... n += 1
....3

However, I would prefer the generator as Aidan has shown because it doesn't
rely on a global variable.

Peter
 
B

Bruno Desthuilliers

Peter Otten a écrit :
... global n
... try:
... return n
... finally:
... n += 1
...

The same without a global:

def f(_n=[0]):
try:
return _n[0]
finally:
_n[0] += 1


But yeps, using a generator would be better.
 
S

Stefan Schwarzer

Hi,

I may be having a brain fart, but is it at all possible to have a
function first return a value then continue its calculation. Like this
simple example:

my_var = 5
def my_function():
return my_var
my_var +=1

This obviously won't work as written but is there a cleaver way around this.

At least in CPython 2.6.5 the above code results in

Traceback (most recent call last):
File "test.py", line 7, in <module>
my_function()
File "test.py", line 3, in my_function
return my_var
UnboundLocalError: local variable 'my_var' referenced before assignment

as soon as the function is called.

If you want to have the global my_var modified, you need
a "global my_var" statement in the function body.

Stefan
 
D

Dennis Lee Bieber

def my_function():
temp = my_var
my_var += 1
return temp

Will fail on the binding to temp, since lacking a global declaration
the subsequent statement defines a local.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top