472,988 Members | 2,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,988 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 4216
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

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

Similar topics

2
by: Joel Forrest Moxley | last post by:
Greetings python-list! The good news is that I've been having a blast with Python since early Spring. I've had great success in both learning the language from online / usegroup resources and...
1
by: Joel | last post by:
python-list@python.org at Sun, 24 Aug 2003 13:31:58 but didn't seem to show up here] Greetings python-list! The good news is that I've been having a blast with Python since early Spring. ...
3
by: Nils Grimsmo | last post by:
hi, i'm having some trouble nesting functions. consider the following: def h(): x = 1 def g(): print x # ok, x is taken from h g()
6
by: Andy Baker | last post by:
Hi there, I'm learning Python at the moment and trying to grok the thinking behind it's scoping and nesting rules. I was googling for nested functions and found this Guido quote:...
15
by: Xah Lee | last post by:
Here's the belated Java solution. import java.util.List; import java.util.ArrayList; import java.lang.Math; class math { public static List range(double n) { return range(1,n,1); }
78
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only...
4
by: Wolfgang Draxinger | last post by:
If you know languages like Python or D you know, that nested functions can be really handy. Though some compilers (looking at GCC) provide the extension of nested functions, I wonder, how one...
0
by: Maric Michaud | last post by:
Le Tuesday 12 August 2008 23:15:23 Calvin Spealman, vous avez écrit : I was not aware of any "nested classes are unsupported" before and didn't consider nested classes as bad practice till...
13
by: Fredrik Lundh | last post by:
Patrol Sun wrote: so why exactly are you trying to nest 20 or 100 for-in loops? </F>
9
by: Gabriel Rossetti | last post by:
Hello, I can't get getattr() to return nested functions, I tried this : .... def titi(): .... pass .... f = getattr(toto, "titi") .... print str(f) .... Traceback...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.