Python Flashcards
How is memory allocation and garbage collection handled in python?
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.
Difference between tuple and list?
- Lists are mutable, allowing you to add,remove, or modify, tuples once created are immutable
-lists are generally slower than tuples
What is Lambda function
Small anonymous function defined using the lambda keyword
Explain *args and **kwargs in python
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.
what are decoraters in python
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).
what are python namespaces
a namespace is a container that holds a set of identifiers and maps them to objects.
example: ‘len’ ‘print’ ‘str’
What are generators
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 to implement concurrency. in python?
- 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.
What is GIL?
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 to optimize a python application?
-efficient data structures
-leverage built in functions and libraries
-implement caching
-parallel processing.
- optimize memory usage
What is monkey patching?
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.
What are classes in python?
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.