How to pass out the result from iterated function

J

JD

I got a iterated function like this:

def iterSomething(list):
has_something = False
for cell in list:
if something in cell:
has_something = True
output = something
if has_something:
iterSomething(output)
else:
final_out = outupt

The problem is how can I read this final_out outside of the function.
I tried the global statement, it seems not work. Any idea?

JD
 
E

eric

I got a iterated function like this:

def iterSomething(list):
    has_something = False
    for cell in list:
        if something in cell:
            has_something = True
            output = something
   if has_something:
       iterSomething(output)
   else:
       final_out = outupt

The problem is how can I read this final_out outside of the function.
I tried the global statement, it seems not work. Any idea?

JD

why don't you just return it ?


def iterSomething(list):
has_something = False
for cell in list:
if something in cell:
has_something = True
output = something
if has_something:
return iterSomething(output)
else:
return output

?
 
G

Gary Herron

JD said:
I got a iterated function like this:

def iterSomething(list):
has_something = False
for cell in list:
if something in cell:
has_something = True
output = something
if has_something:
iterSomething(output)
else:
final_out = outupt

The problem is how can I read this final_out outside of the function.
I tried the global statement, it seems not work. Any idea?

Without examining the intent of your code, I'll suggest this:

if has_something:
return iterSomething(output)
else:
return output

So either way, *something* is returned, and in the case of the recursive call, the innermost result is returned back up through all levels of the recursion.

Is that what you wanted?

Gary Herron
 
J

James Stroud

JD said:
I got a iterated function like this:

def iterSomething(list):
has_something = False
for cell in list:
if something in cell:
has_something = True
output = something
if has_something:
iterSomething(output)
else:
final_out = outupt

The problem is how can I read this final_out outside of the function.
I tried the global statement, it seems not work. Any idea?

JD

I don't think this function will iterate the way you intend. You
probably won't get many helpful suggestions until you define more
precisely the nature of alist. Right now the function assumes single
item lists only and will fail with an exception if something is not
contained in the deepest list. E.g., only this type of situation is allowed:

[[somethign]]

or

[[[something]]]

or

[[[[something]]]]

etc.

Anything else gives an exception.

For example, this will fail with an exception:

[[1,2,3], [4,5,6], [7,8,9]]

Is that really what you want?

Hint: you need to test whether you can iterate over the elements alist.

Also, use something like "alist" and not "list" because "list" is a
reserved word.

James
 
R

Rhodri James

I got a iterated function like this:

def iterSomething(list):
has_something = False
for cell in list:
if something in cell:
has_something = True
output = something
if has_something:
iterSomething(output)
else:
final_out = outupt

The problem is how can I read this final_out outside of the function.
I tried the global statement, it seems not work. Any idea?

Isn't this going to throw an exception anyway if 'something' doesn't
appear in 'list'? You try assigning 'output' to 'final_out' with no
guarantee that 'output' has ever been assigned to.
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top