471,338 Members | 993 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

nested functions in python

i hav created a function(say solve(parameters)) within a function(say display(parameters)).... then the python code goes sumwat like this:
Expand|Select|Wrap|Line Numbers
  1.  
  2. def display(parameter1, parameter2):
  3.  
  4. {code of display}
  5.  
  6. solve(parameter3, parameter4):
  7.  
  8.  
  9. {code of solve and it gives me its output in the form of a number}
  10.  
  11. #now i want to return control to display
  12.  
  13. {remaining code of display continues...}
  14.  
how do i pass control from 1 function 2 another... like in this example do i need 2 say return(in the solve function) or sumthing like tht?
Jul 11 '07 #1
4 4162
elbin
27
i hav created a function(say solve(parameters)) within a function(say display(parameters)).... then the python code goes sumwat like this:
Expand|Select|Wrap|Line Numbers
  1.  
  2. def display(parameter1, parameter2):
  3.  
  4. {code of display}
  5.  
  6. solve(parameter3, parameter4):
  7.  
  8.  
  9. {code of solve and it gives me its output in the form of a number}
  10.  
  11. #now i want to return control to display
  12.  
  13. {remaining code of display continues...}
  14.  
how do i pass control from 1 function 2 another... like in this example do i need 2 say return(in the solve function) or sumthing like tht?
You define ALL functions separately from one another before calling them with the parameters:

Expand|Select|Wrap|Line Numbers
  1. def display(parameter1, parameter2):
  2.  
  3.     {code of display}
  4.  
  5.     A = solve(parameters)
  6.  
  7.     {rest of code of display}
  8.  
  9. def solve(parameter3, parameter4):
  10.  
  11.     {code of solve}
  12.  
  13.     return number
  14.  
  15. display(x, y) 
  16.  
When solve() is called inside display() it runs with the given parameters, and then returns the value of the number you want to display, which assigns it to A. If it doesn't return anything and just runs - you just call it without assigning.
Jul 11 '07 #2
bartonc
6,596 Expert 4TB
i hav created a function(say solve(parameters)) within a function(say display(parameters)).... then the python code goes sumwat like this:
Expand|Select|Wrap|Line Numbers
  1.  
  2. def display(parameter1, parameter2):
  3.  
  4. {code of display}
  5.  
  6. solve(parameter3, parameter4):
  7.  
  8.  
  9. {code of solve and it gives me its output in the form of a number}
  10.  
  11. #now i want to return control to display
  12.  
  13. {remaining code of display continues...}
  14.  
how do i pass control from 1 function 2 another... like in this example do i need 2 say return(in the solve function) or sumthing like tht?
I skipped over this the first time because I was in a hurry and because I hate seeing nested function.

The answer lie in the "scope rules". Since function2() is def(inede) inside function1() on, it call be called ONLY from within function1()
Expand|Select|Wrap|Line Numbers
  1. def func1(*args, **kwarg):
  2.     def func2(arg1, arg2):
  3.          print arg1, arg2
  4.          return results
  5.     # do some work to make some args
  6.     intermediateResuts = func2(val1, val2)
  7.     return results
Jul 11 '07 #3
bartonc
6,596 Expert 4TB
You define ALL functions separately from one another before calling them with the parameters:

Expand|Select|Wrap|Line Numbers
  1. def display(parameter1, parameter2):
  2.  
  3.     {code of display}
  4.  
  5.     A = solve(parameters)
  6.  
  7.     {rest of code of display}
  8.  
  9. def solve(parameter3, parameter4):
  10.  
  11.     {code of solve}
  12.  
  13.     return number
  14.  
  15. display(x, y) 
  16.  
When solve() is called inside display() it runs with the given parameters, and then returns the value of the number you want to display, which assigns it to A. If it doesn't return anything and just runs - you just call it without assigning.
This is, of course the cleaner thing to do, although python's syntax does not require it.
Jul 11 '07 #4
bvdet
2,851 Expert Mod 2GB
i hav created a function(say solve(parameters)) within a function(say display(parameters)).... then the python code goes sumwat like this:
Expand|Select|Wrap|Line Numbers
  1.  
  2. def display(parameter1, parameter2):
  3.  
  4. {code of display}
  5.  
  6. solve(parameter3, parameter4):
  7.  
  8.  
  9. {code of solve and it gives me its output in the form of a number}
  10.  
  11. #now i want to return control to display
  12.  
  13. {remaining code of display continues...}
  14.  
how do i pass control from 1 function 2 another... like in this example do i need 2 say return(in the solve function) or sumthing like tht?
I prefer to keep my nested functions before the code of the encapsulating function:
Expand|Select|Wrap|Line Numbers
  1. def funcMain(a, b):
  2.     def funcNested(c, d):
  3.         # perform some calculations
  4.         result = c + d
  5.         return result
  6.  
  7.     # body of funcMain()
  8.     .............................
  9.     var1 = funcNested(k, m)
  10.     var2 = funcNested(q, r)
  11.     ............................
  12.     return answer
Python built-in 'return' returns control back to the calling function, and in this case passes the result of a calculation.
Jul 11 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Joel Forrest Moxley | last post: by
1 post views Thread by Joel | last post: by
3 posts views Thread by Nils Grimsmo | last post: by
6 posts views Thread by Andy Baker | last post: by
15 posts views Thread by Xah Lee | last post: by
78 posts views Thread by Josiah Manson | last post: by
4 posts views Thread by Wolfgang Draxinger | last post: by
reply views Thread by Maric Michaud | last post: by
13 posts views Thread by Fredrik Lundh | last post: by
9 posts views Thread by Gabriel Rossetti | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.