473,405 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 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 4227
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.