EzDevInfo.com

python interview questions

Top python frequently asked interview questions

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'
        self.Change(self.variable)
        print self.variable

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

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


Source: (StackOverflow)

Advertisements

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)

What is a metaclass in Python?

What are metaclasses? What do you use them for?


Source: (StackOverflow)

How to check whether a file exists using Python

How do I check whether a file exists, using Python, without using a try-catch statement?


Source: (StackOverflow)

Finding the index of an item given a list containing it in Python

For a list ["foo", "bar", "baz"] and an item in the list "bar", what's the cleanest way to get its index (1) 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)

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)

Python - append vs. extend

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


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:
        lock.acquire()
        time.sleep(sleeptime)
        lock.release()
        time.sleep(sleeptime)
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?

@makebold
@makeitalic
def say():
   return "Hello"

which should return

"<b><i>Hello</i></b>"

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)

Parse String to Float or Int

In Python, how can I parse a numeric string like "545.2222" to its corresponding float value, 542.2222? Or parse the string "31" to an integer, 31?

I just want to know how to parse a float string to a float, and (separately) an int string to an int.


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)