Tutor profile: Sandesh C.
Subject: Python Programming
What is the difference between yield and return in python?
Whenever a function is called in python, a local variable state is generated. That is, all the variables pertaining to the called method is initialized. Say we call a simple function returnNumber() as follows: def returnNumber(): num = 0 while True: return num num += 1 The above function will always return 0 since the function returns the value of num in the first iteration of the while loop, and when the function returns, the state of the local variable num is erased, and num += 1 is never executed. However, lets consider the following function instead: def returnNumber(): num=0 while True: yield num num += 1 This function now will yields a generator object instead. Say we call it in the following way: for i in range(returnNumber()): print(i) In the first iteration of this for loop it's easy to see that 0 is printed. But the power of the yield statement is seen in the following iterations. In the following iterations of the for loop 1,2,3,4.... is printed infinitely instead of repeating zeroes. How does this work? When a function yields, the local variables are stored and the next time the same function is called they are thus recovered. So the first time returnNumber() yielded it returned 0 and the second time it was called it resumes at "num += 1", thus updating the value of num. Then it goes into the while loop again and yields a value of 1. This continues infinitely since "while True:" is an infinite loop.
Subject: Computer Science (General)
What is the difference between overriding and overloading in relation to Object Oriented Programming?
Say we have a Tea class that allows creation of Tea objects, and we have a Brew method() that brews whatever object is passed in. For now lets focus on calling Brew() with Tea objects - that is, brewing tea. People like their tea different. We need to able to handle multiple requests like: Brew( Tea("Earl Grey"), SugarLevel = 10) Brew( Tea("Irish Breakfast")) To handle the above situation, we create two methods with the same name. Something like: Brew(type): Brew(type,sugarLevel): Here, the second Brew() method OVERLOADS the first one in the sense that if more than one parameter is given to the Brew() method the second one is executed, else the first one does the job. If we expect situations where the same method in a class is called with different number of parameters, we need to write multiple methods with the same name to handle each one of those cases. This process is called overloading. Now imagine we need a Coffee class that creates coffee objects and calling Brew() on these brews coffee instead. We can inherit the preexisting variables and methods from the Tea class since variables likes waterTemp and brewTime can be shared between the two. However, we might need to rewrite the Brew() method in the Coffee class since coffee might be brewed differently than tea. Thinking in these terms, if we write a new Brew() method in the new Coffee class, when Brew() is called on a Coffee object, the program executes coffee brewing instructions and not the tea one. Here, we have successfully OVERRIDDEN the Brew() method in Tea class. This process of overwriting the preexisting method in a parent class by creating a new method in the child class with the same signature is called overriding.
While riding a bike, if you slam on your front brakes, you might go over the handle bars. Why?
Initially, while the bike is in motion, the two wheels are rotating counter-clockwise and have a total angular momentum directed towards the left (call it the positive direction, and ignore other small contributions to angular momentum from pedals, etc.). Now if you slam on the front brakes, the angular momentum is effectively halved, and since there is no external torque applied to the system (you and the bike), the total angular momentum must be conserved. Due to this principle of conservation of total angular momentum, a force is produced at the back of the bike directed upwards in an attempt to move the whole bike counter-clockwise and consequently preserve the initial angular momentum. And now since the contact point between the front wheel and the ground acts as an effective fulcrum, a force on the back wheel produces a large torque, thus pushing you over the handle bars. To avoid this, experienced bikers get off their saddle and extend their hips behind the saddle, and over the back wheel so that their body itself produces a large torque in the clockwise direction and counters the torque produced via braking.
needs and Sandesh will reply soon.