Tells what and why you would want to use an inner or nested function. This video goes over: some encapsulation, factory functions, and wrapper functions. | Language Python (.py) | Code Example github.com/sim...
Пікірлер: 20
@stephenbouldin81633 жыл бұрын
I cannot believe this isn't way more popular. This is much more complete and usable code
@Ian698855 жыл бұрын
great vedio you explain this way better than a lot of books and my lecturer. Thanks so much
@mahsameymari64114 жыл бұрын
you are a great teacher
@bhoomi_d6 жыл бұрын
@10:00 is that same as decorator?
@Gazumbo4 жыл бұрын
Thank you for the tutorials. I'm not fully understanding the passing of arguments here.. You assigned 'make_is_divisible(2)' to the variable 'is_div_2'. You then passed an argument to 'is_div_2' but I don't understand how 'is_div_2' knows that you want this argument to be passed to the 'num' parameter in 'def is_divisible' as 'is_divisible' isn't called. Would really appreciate some help on this.
@SimplyCoded4 жыл бұрын
# First thing we have to understand is that functions can be given alternate names. So you could do this: def hello(): print("Hello there") # I could give it another name like so greet = hello # I can now call both functions and they'll do the same thing # Both print 'Hello there' hello() greet() # You can do this regardless of if they have parameters. It is the same process as above: def hello(user): print("Hello there", user) greet = hello hello("Jeremy") greet("Jeremy") # You can pass around function names without calling them or running the code by using just the function name without round brackets (). # So you could do this def runMyFunc(greet_user): greet_user("Kate") # In this instance I'm passing in the function 'hello' and renaming it to 'greet_user' and then running it inside the 'runMyFunc' method. runMyFunc(hello) # Now that we know that, we can see that make_is_divisible(2) is just creating a function called is_divisible(num), and returning it's name/pointer. # Now that I have the returned function I can give it a name like 'is_div_2' or any other name, and run it whenever I want. # So when I call make_is_divisible(2) I'm creating the below function def is_divisble(num): return num % 2 == 0 # If I were to instead call make_is_divisible(5) I would be creating the below function. def is_divisble(num): return num % 5 == 0 # Once I return the function I can call it whatever I want. In the video's case I renamed the is_divisible func reference that was returned to me as 'is_div_2' # which means is_div_2 is now the same as the below code because it is the same function as is_divisible just with a '2' instead of a 'den' variable. def is_div_2(num): return num % 2 ==0 Make sense?
@Gazumbo4 жыл бұрын
@@SimplyCoded Thanks so much for taking the time to explain it. I'm pretty sure i understand it now. Basically, by returning the 'is_divisible' function to 'make_is_divisible', it then becomes all one package (or object). When you assign 'make_is_divisible' to 'is_div_2', you also pass it with the argument which is then passed to 'den'. This is the part I hope I'm getting right... As 'num' is still requirement an argument to be passed to it. Python automatically passes the argument passed when calling 'is_div_2' as it's the only part of the function that is still requirement an argument to be passed to it.
@SimplyCoded4 жыл бұрын
It sounds like you got it. The only thing I’m confused about is that I’m not sure what you mean by Python automatically passes the num argument; because it doesn’t, you still have to pass that argument. Calling the function would look like this is_div_2(86), where 86 is the num argument. So no, python doesn’t automatically pass anything.
@Gazumbo4 жыл бұрын
@@SimplyCoded I don't think I explained it very well. Say for example you were to add a 2nd user defined function nested inside 'make_is_divisible' you returned this function to 'make_is_divisible' in the same return statement as 'is_divisible'. When you call 'is_div_2', how does Python know which function you want to pass the argument to? I tried to test this myself by adding the following code but I got a Type error: 'Tuple object is not callable'. I added the following code inside 'make_is_divisible': -- def is_not_divisible(num2): -- return num2 % den != 0 -- return is_divisible, is_not_divisible I hope that makes sense.
@SimplyCoded4 жыл бұрын
@@Gazumbo If you ever think 'How does insert_programming_language_here know that you want to do xyz' you should know the answer is always 'It does NOT know what you want it to do; it will not automatically do anything for you. You have to specify exactly what you want the code to do within the confines of what the language can do.' With that being said, what you're doing is returning a Tuple, that means if you want one of the values from that tuple (i.e. you want to specify which function you want from the two that you returned) you'll have to index to that one. #you are returning a tuple so you need to index the correct value you want def make_is_divisible(den): def is_divisible(num): return num % den == 0 def is_not_divisible(num): return num % den != 0 return is_divisible, is_not_divisible tup_div_2 = make_is_divisible(2) #index 0 will be first func, index 1 will be second func print('The number 84 is even:', tup_div_2[0](84) ) print('The number 84 is not even:', tup_div_2[1](84) ) #or you could assign each returned func to another func name tup_div_2 = make_is_divisible(2) is_div_2 = tup_div_2[0] is_not_div_2 = tup_div_2[1] print('The number 84 is even:', is_div_2(84) ) print('The number 84 is not even:', is_not_div_2(84) ) #because your two functions are opposite I would highly advice only returning one and using the 'not' operator to check for the opposite case def make_is_divisible(den): def is_divisible(num): return num % den == 0 return is_divisible is_div_2 = make_is_divisible(2) print('The number 84 is not even:', not is_div_2(84) )
@yaoli3811 Жыл бұрын
Why don't just put codes of wrapper function inside time_this, why do we need to create a wrapper function just to return it?
@SimplyCoded Жыл бұрын
The inner function is so that you can return a reference to a function rather than just calling the function inside of another one right away So you could totally have a time this function with no additional wrapper where every time you wanted to use it you would use time this and then pass in the function you want to time and it would be immediately ran and timed. The benefit of a wrapper is being able to return the function with additional features, and run it at a later time. it lets you basically create functions dynamically with different data inside of them that you won't have to pass in via the arguments because they're saved when creating the wrapper This syntax also allows you to do some cool syntax that I cover in the next video
@yaoli3811 Жыл бұрын
@@SimplyCoded totally make sense now, thanks a lot!
@HappyAnimals3D6 жыл бұрын
Subscribed
@georgesmith30225 жыл бұрын
is this Bo?
@SimplyCoded5 жыл бұрын
Nope
@ajinmohamed17077 жыл бұрын
Can a u ples help me make a python keylogger virus I will play you
@ajinmohamed17077 жыл бұрын
(pay)
@ajinmohamed17077 жыл бұрын
I will pay yu 1000000 indian rupees
@SimplyCoded7 жыл бұрын
+Ajin Mohamed Doubt it. And no thanks not interested. I'm against viruses.