Skip to content

Instantly share code, notes, and snippets.

@emre
Last active July 8, 2019 14:24
Show Gist options
  • Save emre/0d4491a4eb3eb6df068284bfe0af299e to your computer and use it in GitHub Desktop.
Save emre/0d4491a4eb3eb6df068284bfe0af299e to your computer and use it in GitHub Desktop.
Python interview questions

What will be the output? (LUX)

c = 3

def add():
    c = c + 2
    return c
    
add()

c is not defined in the function's scope, thus:

UnboundLocalError: local variable 'c' referenced before assignment

What will be the output? (LUX)

class A:
    def get_balance(self):
        return 1
      
class B:

def get_balance(self):
        return 2
      
      
class C(A, B):
    pass

c = C()
print(c.get_balance())

Keyword: MRO (Method resolution order)

sets are mutable or immutable? (LUX)

  • set() -> mutable

  • Frozenset -> immutable

Do multiple Python threads execute at the same time? (LUX)

No. Because of GIL.

Workarounds for GIL? (LUX)

Multiprocessing.

Why GIL exists? (LUX)

Thanks to GIL, There is no need to write thread-safe programs most of time. No need for locks, semaphores, and thread syncronization primivites.

What happens if multiple threads share the same variable? (E.G: getting an integer value and incrementing it by one) (LUX)

...

What do you like about Python3?

...

What's the most annoying thing while porting python2 programs to python3? (LUX)

String/Unicode/Byte compability.

Bytes/string literals

Prefix Python 2 strings that are meant to contain bytes with a b prefix to very clearly delineate what is and is not a Python 3 text > > string (six provides a function to use for Python 2.5 compatibility). This point cannot be stressed enough: make sure you know what all of your string literals in Python 2 are meant to be in Python 3. Any > string literal that should be treated as bytes should have the b prefix. Any string literal that should be Unicode/text in Python 2 ?> should either have the u literal (supported, but ignored, in Python 3.3 and later) or you should have from future import unicode_literals at the top of the file. But the key point is you should know how Python 3 will treat every one one of your string literals and you should mark them as appropriate. There are some differences between byte literals in Python 2 and those in Python 3 thanks to the bytes type just being an alias to str > in Python 2. See the Handle Common “Gotchas” section for what to watch out for.

Time complexify of accessing an element of a dictionary

Check this.

What is Polymorphism? (LUX)

...

How Python's garbage collection works?

  • Python maintains a count of the number of references to each object in memory. If a reference count goes to zero then the associated object is no longer live and the memory allocated to that object can be freed up for something else occasionally things called "reference cycles" happen.

  • The garbage collector periodically looks for these and cleans them up. An example would be if you have two objects o1 and o2 such that o1.x == o2 and o2.x == o1. If o1 and o2 are not referenced by anything else then they shouldn't be live. But each of them has a reference count of 1.

  • Certain heuristics are used to speed up garbage collection. For example, recently created objects are more likely to be dead. As objects are created, the garbage collector assigns them to generations. Each object gets one generation, and younger generations are dealt with first.

Difference between tuples and lists? (Example use cases) (LUX)

Lists are mutable, Tuples are not.

How are arguments are passed in Python? Pass by value or Pass by reference? (LUX)

It's a mix depending of the type of the argument.

Explain what is PEP8 and what is it good for?

...

What are Python decorators? Did you ever use them? Explain a use case. (LUX)

...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment