Python interview questions are designed for freshers & experienced professionals to help to prepare for upcoming Python Interviews.
Python Interview Questions
Where is the math.py (socket.py, regex.py, etc.) source file?
To quickly check where a module is located, import it and check the module’s __file__ attribute:
>>> import math >>> math.__file__ '/usr/lib/python2.4/lib-dynload/math.so' >>> import random >>> random.__file__ '/usr/lib/python2.4/random.pyc'
If this gives you an AttributeError exception, the module is built into the interpreter.
What is the key difference between a List and a Tuple?
The main difference is that a List is mutable in Python whereas Tuple is immutable. And from memory consumption perspective, a List consumes more memory than a Tuple.
How to Call Base Class Method From a Derived Class?
For old-style classes:
BaseClassName.methodname(self, arguments)
>>> class A: ... def hello(self): ... print 'a' ... >>> A().hello() a >>> class C(A): ... def hello(self): ... A.hello(self) ... print 'c' ... >>> C().hello() a c
For new-style class:
super(ClassName, self).method(args)
>>> class M(object): ... def hello(self): ... print 'm' ... >>> class N(M): ... def hello(self): ... super(N, self).hello() ... print 'n' ... >>> N().hello() m n