0
Nested function
Can someone explain when and why should i use the below nested function in place of the more seemingly efficient print function? num=2; print(num, num+2) def func(x): def nested_func(x): return x + 2 var=nested_func(x) print(x, var) func(2)
7 Respostas
+ 3
You use functions to structure your code into sub-codes for easier maintenance and reusability.
Practice examples often don't explain this well enough. They show a function like this:
def add(x, y):
return x+y
And the beginner asks: Why can't I just write x+y directly?
And yes, you can, and you should!
A function makes sense when you save yourself from writing the same piece of code over and over. For this, it should be an actual piece of code.
For example you could have a function that takes a regular string and reformats it in a way that it looks formatted, a bit more like a page in a book.
That would be a lot of code you'd have to rewrite every time you format a string!
Then it starts to make sense to make a function for it, and from then just write:
format(my_string)
Embedded functions are a special case. From your question I sensed a certain confusion with functions in general. If this isn't enough, please tell.
+ 2
Thanks for the reply, your points make sense and it's more clear now.
+ 2
Your function is just an example to show you can have a function inside a function ( i assume it's from a tutorial somewhere), which will probably lead you on to "closures", where an outer function returns the inner function.
+ 2
to add to the excellent explanation of HonFu
I also use it to âlay outâ a programme by which I mean I start off by writing the main function and then work out what functions would need to go in that main function to make it work. then I go into the smaller functions and write function names that I think I need to make that (the smaller) function work.
for example a programme that has a robot pick up and move cargo:
main() {
robot();
cargo();
}
robot(){
createRobot();
move();
pickUp();
dropOff();
}
cargo(){
createCargo();
move(); or moveCargo();
}
createRobot() {
//here I would write the base code without using custom functions to draw the robot and where it starts on the screen
}
this of course is somewhat oversimplified, but what it helps to do is that I map out what I need and then can start writing code in the âsmallestâ function.
also I can easily reuse the functions if it needs to be used elsewhere, or simply edit 1 function instead of an entire code.
+ 1
Brave Tea, that's interesting, so you use functions to plan out your code, like writing an exposé?
I tend to go the other direction, starting from the small stuff first and let them grow together.
A little typing trainer I made consists of a main 'player' function that will present a set of words to the user for practice.
Then there are several assistant functions that work with the words and sort and assemble them by different standards, like: All words with q in it or something like that.
My first impulse would be to start right with these, so that the small stuff is out of the way. So after the assistance functions return the right word collections as I want them, I'd go to the next part and see how exactly I'd work with them.
I also learn like that, from small to big. Sources, sites or books, that just slam it all in your face at once don't work for me. I want to get the (practical) details first and then watch them assemble to something bigger.
+ 1
HonFu yeah sort of. but to be honest we sort of do the same thing. difference is I write it down in functions and I am guessing you have a broad idea of what you want to do in your mind instead of on paper.
and I think that my initial expose only houses the bare minimum functions. so no assistent functions.
for example.
the robot I wrote about earlier:
today I added a limiter (); and a goal(); function and it was easy to do by just writing a new function and putting it in the robot() code. these are add ons or assistent functions. I donât need them at first and werenât in my initial expose. now they are. but my code wouldnât function without a robot(); and drawRobot() function so I need that straight away.
I still start at the small stuff though. once I have written out the functions I start filling in the âsmallestâ one
+ 1
Yeah, I also usually have an idea. Sometimes I make a draft on paper that may look to other people like I finally lost it.
Girlfriend coming home, she's like: 'Ah, mad professor mode again.' đ€Ł
But if the structure looks too complete in the beginning, I'll have trouble letting go of it when I find out it doesn't work and I need to switch things up. So I 'outsource' my planning to paper, preferably the backsides of envelopes or something like that.
So, yeah, probably it has to be induction and deduction alternatingly working hand in hand...