Interview-Questions-Python
- How Python types can be splitted in two categories?
- Immutable types(tuple, str, int, float) We can't change the value of
immutable type.
The value of the variable changes, but it changes by changing what the variable refers to. A mutable type can change that way, and it can also change "in place".
Consider following piece of code:lst = [] s = "" print(id(lst), id(s)) lst += [1] s += 'bar' print(id(lst), id(s))
The value of id(lst) remains the same, while the id(s) changes because it refers to a new object.
- Mutable Types(list, dict, set) We can change the value of mutable type "in place".
- Immutable types(tuple, str, int, float) We can't change the value of
immutable type.
-
What's decorator in Python? What the decorator should return? What are they
used for?
A Python decorator is a specific change to the Python syntax that allows us to more conveniently alter functions and methods (and possibly classes in a future version). This is ideal when you need to extend the functionality of functions that you don't want to modify.
Decorator should return a "decorated" object. Actually, decorator is just a syntax sugar that can be changed to the following piece of code:@change def f(*args, **kwargs): pass
Is identical to:
def f(*args, **kwargs): pass f = change(f)
In Python function are first class objects, they can be assigned, passed to another functions as arguments etc. Another important property to understanding decorators is closure . Which actually means that: Inner functions have access to the enclosing scope
https://www.thecodeship.com/patterns/guide-to-python-function-decorators/
- What is generator? How it differs from iterator?
- What's average lookup time in Python dict? Why can't we use mutable types as keys of dict?
- What are metaclasses? What methods they should implement? Prepare method in metaclasses
- What is Singleton? How would you implement it in Python?
- How is implemented Python attribute access?
- How modules are imported in Python? importlib What will be the result of the following code?
- How Python subclasses work? MRO
-
What will the following code produce?
class A(object): x = 1 class B(A): pass class C(A): pass print A.x, B.x, C.x B.x = 2 print A.x, B.x, C.x A.x = 3 print A.x, B.x, C.x
a = [0]
lst = [a]*5
lst[0].append(1)