Python Interview Questions and Answers Set 9

81. Why does Python have a maximum recursion depth?

Recursion requires space on the call stack, which is limited in size. Code which used too many levels of recursion will give an error called a stack overflow. Python stack frames are also quite big in size which further makes the issue more crucial.

82. Can you modify the maximum depth for a recursive function in Python? If yes how?

Yes

sys.setrecursionlimit(1500) // Generally the length is 1000 stack frame

83. What is tail recursion?

In traditional recursion, the typical model is that you perform your recursive calls first, and then you take the return value of the recursive call and calculate the result. In this manner, you don’t get the result of your calculation until you have returned from every recursive call.

In tail recursion, you perform your calculations first, and then you execute the recursive call, passing the results of your current step to the next recursive step. This results in the last statement being in the form of “(return (recursive­function params))” . Basically, the return value of any given recursive step is the same as the return value of the next recursive call.

The consequence of this is that once you are ready to perform your next recursive step, you don’t need the current stack frame any more. This allows for some optimization. In fact, with an appropriately written compiler, you should never have a stack overflow snicker with a tail recursive call. Simply reuse the current stack frame for the next recursive step.

84. Does python perform tail recursion optimization?

No it doesn’t. Python founder Guido van Rossum wrote in his blog

“I recently posted an entry in my Python History blog on the origins of Python’s functional features. A side remark about not supporting tail recursion elimination (TRE) immediately sparked several comments about what a pity it is that Python doesn’t do this, including links to recent blog entries by others trying to “prove” that TRE can be added to Python easily. So let me defend my position (which is that I don’t want TRE in the language). If you want a short answer, it’s simply unpythonic”.

85. What is a metaclass in Python?

A metaclass is the class of a class. Like a class defines how an instance of the class behaves, a metaclass defines how a class behaves. A class is an instance of a metaclass.

86. How to get class name of an instance in Python?

instance.class.__name__

87. Describe how to use Sessions for Web python?

Sessions are the server side version of cookies. While a cookie preserves state at the client side, sessions preserves state at server side.

The session state is kept in a file or in a database at the server side. Each session is identified by a unique session id (SID). To make it possible to the client to identify himself to the server the SID must be created by the server and sent to the client whenever the client makes a request.

88. Multiply all elements of a list without writing a loop.

from operator import mul

reduce(mul,range(1,10))

89. What is a singleton design pattern?

In the singleton design pattern that limits the number of instances of a class (normally to 1).

90. Write a program to show the usage of singleton pattern in Python? Ans. Some code along the following lines would do.

Singleton&Singleton::Handle() {

if(!psingle ){

psingle =newSingleton;

}

return*psingle;

}