Python Interview Questions and Answers Set 8

71. Explain how to overload constructors (or methods) in Python.?

_init__ () is a first method defined in a class. when an instance of a class is created, python calls __init__() to initialize the attribute of the object. Following example demonstrates further:

classEmployee:

def__init__(self,name,empCode,pay):

self.name=name

self.empCode=empCode

self.pay=pay

e1 =Employee(“Obama”,99,30000.00)

e2 =Employee(“Clinton”,100,60000.00)

print(“Employee Details:”)

print(” Name:”,e1.name,”Code:”,e1.empCode,”Pay:”,e1.pay)

print(” Name:”,e2.name,”Code:”,e2.empCode,”Pay:”,e2.pay)

—————————————————————

Output

EmployeeDetails:

(‘ Name:’,’Obama’,’Code:’,99,’Pay:’,30000.0)

(‘ Name:’,’Clinton’,’Code:’,100,’Pay:’,60000.0)

72. How do we make python scripts executable?

Python scripts can be executed in two ways:

Suppose we want to execute script1.py

We can open the script1.py in IDE editor & run the script in the frontmost window of the python IDE by hitting the run all button.

Second way is using command prompt by making sure PATH is set appropriately directly type script name else type

>>>python script1.py

73. We have the following code with unknown function f()

for x in f(5):

print x

Output looks like this

0182764

74. What is Pickling and unpickling? 

Pickle is a standard module which serializes & de­serializes a python object structure. pickle module accepts any python object converts it into a string representation & dumps it into a file(by using dump() function) which can be used later, process is called pickling. Whereas unpickling is process of retrieving original python object from the stored string representation for use.

75. Write the function f() ?

Following is a possible implementation of f()

deff(n):

for x in range(n):

yield x**3

76. What are some common uses of Pickling in Python?

These are some of the use cases, there can be many other:­

Saving a program’s state data to disk so that it can carry on where it left off when restarted (persistence)

Sending python data over a TCP connection in a multi­core or distributed system (marshalling)

Storing python objects in a database

Converting an arbitrary python object to a string so that it can be used as a dictionary key (e.g. for caching & memoization)

There are some issues with the last one ­ two identical objects can be pickled and result in different strings ­ or even the same object pickled twice can have different representations. This is because the pickle can include reference count information

77. Why do list comprehensions write to the loop variable, but generators don’t? 

This was a bug in Python 2.x and is fixed in Python 3.x.

78. What is the length of your largest python code? Can you please describe the project?

It’s a very open ended question. The answer is difficult to predict. Python beginners will have written small codes (basic scripts). More advanced users would have used OOP in python and their codes will range from a 200 to 800 lines of code. Advanced users will have written larger codes upto 2000 or 3000 lines of codes.

Beyond this point, codes are generally written in groups to meet a deadline.

79. In the above project, were you alone or were you in group of developers? If so, how many?

(Open Ended Question) Depends on the personal experience of the person. A good successful project being part of the team indicates that the person is a good team player.

80. What was your contribution to the above project?

(Open ended Question). Interviewee’s answer depends on the personal experience which could involve writing a module for the project, testing or documentation etc.

How often do you comment your code? Can you write anyone who reads your code recognise and understand your variable names?