Python Flashcards

1
Q

How is memory allocation and garbage collection handled in python?

A

Both memory allocation and garbage collection are handled discretely.
Memory allocation - “heap” is the pool of memory for storing objects, python memory manager allocates and de allocates space as needed.

garbage collection - python employs a method. called reference counting along with a cycle-detecting garbage collector.
-every object has a reference count, when an object’s count drops to zero it is immediately de-allocated.

developer isn’t directly responsible for memory allocation or de-allocations

level of memory efficiency isn’t as high as that of c. or c++ because python is designed to be convenient and easy to use often at expense of some performance optimization.

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

Difference between tuple and list?

A
  • Lists are mutable, allowing you to add,remove, or modify, tuples once created are immutable
    -lists are generally slower than tuples
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is Lambda function

A

Small anonymous function defined using the lambda keyword

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

Explain *args and **kwargs in python

A

In Python, *args and **kwargs are often used to pass a variable number of arguments to a function.

*args collects a variable number of positional arguments into a tuple, while **kwargs does the same for keyword arguments into a dictionary.

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

what are decoraters in python

A

In Python, a decorator is a design pattern and a feature that allows you to modify functions and methods dynamically. This is done primarily to keep the code clean, maintainable, and DRY (Don’t Repeat Yourself).

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

what are python namespaces

A

a namespace is a container that holds a set of identifiers and maps them to objects.
example: ‘len’ ‘print’ ‘str’

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

What are generators

A

Generators are a type of iterable in Python, like lists or tuples, but they don’t store all of their elements in memory. Instead, they generate each value on the fly, making them more memory-efficient, especially for large data sequences

Generators are created using functions and the yield keyword or through generator expressions.

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

How to implement concurrency. in python?

A
  • Threading is ideal for tasks that spend a lot of time waiting
  • Thread Pooling with concurrent.futures.ThreadPoolExecutor: Thread pooling is useful when you need to manage a large number of threads.
  • Python’s multiprocessing module creates separate processes, bypassing the GIL and making it suitable for CPU-bound tasks.
    -asyncio is Python’s built-in library for asynchronous programming, using async/await syntax and event loops to handle I/O-bound tasks without blocking.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is GIL?

A

the Global Interpreter Lock is a mutex mechanism that allowes only one thread to execute the python bytecode at a time, this lock is necessary because. CPythons memory management is not thread safe.

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

how to optimize a python application?

A

-efficient data structures
-leverage built in functions and libraries
-implement caching
-parallel processing.
- optimize memory usage

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

What is monkey patching?

A

In Python, monkey patching refers to dynamically modifying or extending a class or module at runtime. This technique allows you to alter or add new methods and attributes to existing classes or modules without modifying the original source code.

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

What are classes in python?

A

In Python, classes are templates for creating objects. A class defines the structure and behavior (attributes and methods) that the objects created from it will have. Classes are a foundational concept in object-oriented programming (OOP), which allows for modular and reusable code.

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