python interview questions

Top python frequently asked interview questions

How to list all files of a directory in Python

How can I list all files of a directory in python and add them to a list?

Source: (StackOverflow)

What is the difference between @staticmethod and @classmethod in Python?

What is the difference between a function decorated with @staticmethod and one decorated with @classmethod?

Source: (StackOverflow)

How do I pass a variable by reference?

The Python documentation seems unclear about whether parameters are passed by reference or value, and the following code produces the unchanged value 'Original'

class PassByReference:
    def __init__(self):
        self.variable = 'Original'
        print self.variable

    def Change(self, var):
        var = 'Changed'

Is there something I can do to pass the variable by actual reference?

Source: (StackOverflow)

What IDE to use for Python?

What IDEs ("GUIs/editors") do others use for Python coding?

Source: (StackOverflow)

Explain Python's slice notation

Do you have a good explanation (with references) on Python's slice notation? To me, this notation needs a bit of picking up. It looks extremely powerful, but I haven't quite got my head around it and am looking for a good guide.

Source: (StackOverflow)

In Python, check if a directory exists and create it if necessary

What is the most elegant way to check if the directory a file is going to be written to exists, and if not, create the directory using Python? Here is what I tried:

filename = "/my/directory/filename.txt"
dir = os.path.dirname(filename)


f = file(filename)

Somehow, I missed os.path.exists (thanks kanja, Blair, and Douglas). This is what I have now:

def ensure_dir(f):
    d = os.path.dirname(f)
    if not os.path.exists(d):

Is there a flag for "open", that makes this happen automatically?

Source: (StackOverflow)

Understanding Python super() with __init__() methods [duplicate]

This question already has an answer here:

I'm trying to understand super(). From the looks of it, both child classes can be created just fine. I'm curious as to what difference there actually is between the following child classes:

class Base(object):
    def __init__(self):
        print "Base created"

class ChildA(Base):
    def __init__(self):

class ChildB(Base):
    def __init__(self):
        super(ChildB, self).__init__()


Source: (StackOverflow)

Catch multiple exceptions in one line (except block)

I know that I can do:

    # do something that may fail
    # do this if ANYTHING goes wrong

I can also do this:

    # do something that may fail
except IDontLikeYourFaceException:
    # put on makeup or smile
except YouAreTooShortException:
    # stand on a ladder

But if I want to do the same thing inside two different exceptions, the best I can think of right now is to do this:

    # do something that may fail
except IDontLIkeYouException:
    # say please
except YouAreBeingMeanException:
    # say please

Is there any way that I can do something like this (since the action to take in both exceptions is to say please):

    # do something that may fail
except IDontLIkeYouException, YouAreBeingMeanException:
    # say please

Now this really won't work, as it matches the syntax for:

    # do something that may fail
except Exception, e:
    # say please

So, my effort to catch the two distinct exceptions doesn't exactly come through.

Is there a way to do this?

Source: (StackOverflow)

How can I represent an 'Enum' in Python?

I'm mainly a C# developer, but I'm currently working on a project in Python.

How can I represent the equivalent of an Enum in Python?

Source: (StackOverflow)

Does Python have a ternary conditional operator?

If not, is it possible to simulate one using other language constructs?

Source: (StackOverflow)

Python - append vs. extend

What's the difference between the list methods append() and extend()?

Source: (StackOverflow)

Add key to a dictionary in Python?

Is it possible to add a key to a Python dictionary after it has been created? It doesn't seem to have an .add() method.

Source: (StackOverflow)

What does `if __name__ == "__main__":` do?

What does the if __name__ == "__main__": do?

# Threading example
import time, thread

def myfunction(string, sleeptime, lock, *args):
    while 1:
if __name__ == "__main__":
    lock = thread.allocate_lock()
    thread.start_new_thread(myfunction, ("Thread #: 1", 2, lock))
    thread.start_new_thread(myfunction, ("Thread #: 2", 2, lock))

Source: (StackOverflow)

How can I make a chain of function decorators in Python?

How can I make two decorators in Python that would do the following?

def say():
   return "Hello"

which should return


I'm not trying to make HTML this way in a real application, just trying to understand how decorators and decorator chaining works.

Source: (StackOverflow)

Calling an external command in Python

How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?

Source: (StackOverflow)