Python Flashcards
What is MRO?
Method Resolution Order - the order in which base classes are searched when looking for a method or an attribute. In Python the MRO for most cases is depth first then left to right and remove all duplicates except the last one.
What is the output?
class First(object):
def __init__(self):
super(First, self).__init__()
print(“first”)
class Second(object):
def __init__(self):
super(Second, self).__init__()
print(“second”)
class Third(First, Second):
def __init__(self):
super(Third, self).__init__()
print(“third”)
second
first
third
What are the steps in creating an instance of a class?
Python calls its __new__ method to create the object then calls __init__ on the object that is returned. The __new__ looks like this:
def __new__(cls):
return super().__new__(cls)
If new returns an inst of a diff class, then __init__ is not called
What should init return?
None, anything else will throw an error
Add unit property to immutable built-in float.
class MyFloat(float):
def __new__(cls, value, unit):
instance = super().__new__(cls, value)
## customize here
instance.unit = unit
## return instance
return instance
## in immutable types value is set in __new__, so overwriting __init__ with unit would cause error
What are 2 ways to create a singleton?
- Module level constant
- overwrite __new__ (__init__ won’t get called)
What is variable casting?
A cast is a way of explicitly informing the compiler that you intend to make the conversion and that you are aware that data loss might occur, or the cast may fail at run time.
Python is … typed language
dynamically
n = 300
id(n) == ?
returns location of the int object 300 in memory
What is small integer caching?
small integer in python is -5 to 256
At startup of the interpreter session, python creates objects for all small integers. This is done to improve performance (because these objects are used so much)
What is the first class O?
a first-class citizen (also type, object, entity, or value) in a given programming language is an entity which supports all the operations generally available to other entities. These operations typically include being passed as an argument, returned from a function, and assigned to a variable.
What is introspection?
The ability to determine the type of an O at runtime.
What is callable? Which types are callable in Python.
Callable is a type that can be executed. Functions and classes are callable in Python.
What is LEGB rule?
When resolving a name, python looks in 1. local scope, 2. in enclosing function, 3. in global scope, 4. in built-in scope
What are decorators can be used for?
- Prevent duplication of code
Timing how long a function runs - Setting the frequency of a function run
- Memoization - caching of function results, so they don’t have to be called again (ex. 2+2 will always be 4)
- Setting an attribute for multiple unrelated classes without using inheritance.
- Register function as a plugin
What is Big O. What is the best complexity.
A notation used to describe the computational complexity of an algorithm, consists of 2 parts: time and space
the best complexity is O(1)
O stands for order of approximation
What is the difference between integration and acceptance testing?
acceptance testing is performed in the environment that is as close to production as possible