Python Flashcards

1
Q

What is the GIL?

A

It is the Global Interpreter Lock. It is a mutext that allows only one thread to control the Python Interpreter.

This means that only one thread can execute at any time. It’s not a problem in single-threaded programs. It can be a performance bottleneck in multi-threaded applicatons. In short, this mutex is necessary mainly because CPython’s memory management is not thread-safe.

Many potentially blocking or long-running operations, such as I/O, image processing, and NumPy number crunching, happen outside the GIL. Therefore it is only in multithreaded programs that spend a lot of time inside the GIL, interpreting CPython bytecode, that the GIL becomes a bottleneck.

How can multiprocessing work then? GIL only ensures that only one cpython bytecode instruction will run at any given time. So many CPUs can run the instruction – that is up to the kernel.

More information https://www.youtube.com/watch?v=Obt-vMVdM8s

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are Python basic data structures?

A
  1. Dictionaries (dict). They are key-value stores. Also called maps, hashmaps, hashtables. Efficient lookup, insertion and delete.
  2. Lists (list). They are implementation of arrays. Arrays are a data structure that store information in contiguous blocks of memory. It’s very fast to retrieve an element by index. Lists are implemented as dynamic arrays; they automatically grow in size as required. They can also hold arbitrary elements; they don’t have to be of the same type. List also act as decent stacks as they support push and pop’s in amortized O(1) time. They act as very slow queues because inserting/deleting elements at the beggining (append function) is O(n).
  3. Tuple. Another type of array. They are immutable – unlike lists. You can’t add or remove elements to a tuple. Everything has to be defined at creation time. It can be used as a key for a dictionary – lists cannot. It has slightly lower overhead as well.
  4. str (Python 3 only) is an immutable array of unicode characters.
  5. Sets. An unordered collection of objects that does not allow duplicate elements. They allow for cool operations.

EXTRA

  • from array import array. They behave like lists but have a single type. This is not built in.
  • bytes are immutable array of single bytes.
  • bytearray are arrays of bytes
  • frozenset are immutable sets
  • collections.deque for more performante stacks/queues. It’s implemented as a doubly linked lists. Good for adding/removing elements O(1) but slow for accesing random element. queue.Queue is suitable for parallel computing and multiprocessing.Queue for shared jobs.
  • queue.PriorityQueue
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a Class?

A

It is a blueprint or prototype or template from which objects are created. It is used in Object Oriented Programming. It is a means of grouping functionality (methods) and data together.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is an instance?

A

An instance is an unique copy of a class representing an object.

In Python instance methods use the keyword self to access the instance attribute. This instance attribute contains a unique copy of the class’s data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a staticmethod in Python and how does it differ from an instance method? When would you use it?

A

A static method is a Class method that does not need the class to be initialized to be accesed. It does not have access to the instance attribute self since there’s no class instance.

It can be used when the method is a pure function: it does not depend on any class parameters. They can be useful when we need functionality with respect to the class, but not with a particular instance or object of that class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are decorators in Python?

A

A decorator is a design pattern in Python that allows a user to add new functionality to an existing object without modifying its structure.

Python provides the @ syntatic sugar for annotating objects – usually functions – with their decorators. So””

@foo
def bar():

is the same as “foo(bar)””

How well did you know this?
1
Not at all
2
3
4
5
Perfectly