Quick Dictionary - Python
What is a closure in python?
A closure in Python is a function object that remembers values in the enclosing scope even if they are not present in memory.
What is the difference between shallow copy and deep copy?
A shallow copy creates a new object with references to the original elements, while a deep copy creates a new object with copies of the original elements.
What is the purpose of "yield" keyword in python?
The `yield` keyword in Python is used in generator functions to pause execution and return a value to the caller, while maintaining the function's state to resume from where it left off.
What is GIL?
The Global Interpreter Lock (GIL) is a mechanism in Python that ensures only one thread executes Python bytecodes at a time, preventing multiple threads from executing Python instructions simultaneously.
What is the significance of __init__ method?
The `__init__` method in Python classes is a special method that is automatically called when an object is created from a class, allowing you to initialize its attributes or perform other setup actions.
What is difference between list and tuple?
A list is mutable and can be modified, while a tuple is immutable and cannot be changed after creation.
What is MRO?
Method Resolution Order (MRO) in Python determines the order in which methods are searched for and executed in a class hierarchy, following the C3 linearization algorithm.
What is difference between class and variable in python?
A class variable is shared among all instances of a class, while an instance variable is specific to each instance of the class.
What is the role of __str__?
The `__str__` method in Python defines a human-readable string representation of an object and is called by the `str()` function or when using `print()` on an object.
What is the role of super() in python?
The `super()` function in Python is used to invoke a method from a parent class, allowing for inheritance and method overriding in subclasses.
What is difference between "is" and "==" in python?
The `is` operator in Python checks if two objects refer to the same memory location, while the `==` operator compares the values of the objects for equality.
What is the purpose of *args and **kwargs?
The `*args` and `**kwargs` parameters in function definitions allow for variable-length argument passing, where `*args` collects positional arguments and `**kwargs` collects keyword arguments.
What is the difference between instance, class, and static?
Instance methods operate on an instance of a class, class methods operate on the class itself, and static methods are independent of both the class and its instances.
What is the purpose of 'with' statement in python?
The `with` statement in Python is used for resource management, providing a clean way to handle opening and closing of files, database connections, and more.
What is name mangling?
Name mangling in Python is a mechanism that changes the name of a class attribute by adding a prefix of `_ClassName` to avoid accidental conflicts in inheritance.
What is difference between Generator functions and normal functions?
Generator functions in Python generate a sequence of values using the `yield` keyword and can be iterated over, unlike normal functions that return a single value and then terminate.
What is the purpose of None value in python?
The `None` value in Python represents the absence of a value or the null value, often used to indicate the absence of a valid result or uninitialized variable.
What is the purpose of @staticmethod in python?
The `@staticmethod` decorator in Python is used to define a static method in a class, which is a method that belongs to the class itself rather than an instance of the class.
What is the difference between module and package?
A module in Python is a file containing Python code and can be imported and used in other Python programs, while a package is a directory that contains multiple modules and can have a hierarchical structure.
What is the significance of __name__ variable?
The `__name__` variable in Python is a special variable that holds the name of the current module or script, allowing you to determine whether the code is being run as the main program or imported as a module.
What is the difference between set and frozen set?
A set in Python is an unordered collection of unique elements, while a frozenset is an immutable version of a set that cannot be modified once created.
What is monkey patching?
Monkey patching in Python refers to the dynamic modification of a class or module at runtime, allowing you to add, modify, or replace attributes or methods.
What is the difference between shallow and deep comparisons?
Shallow comparison compares the references of objects, while deep comparison compares the values of objects recursively, considering nested objects as well.
What is the purpose of __slots__ in python?
The `__slots__` attribute in Python is used to explicitly define the attributes of a class, saving memory by restricting the instance to only those attributes.
What is the purpose __getitem__ and __setitem__ in java?
The `__getitem__` and `__setitem__` methods in Python allow objects to be accessed or assigned values using indexing and slicing notation.
What is the purpose of append() and extend()?
The `append()` method in Python adds an element to the end of a list, while the `extend()` method adds multiple elements from an iterable to the end of a list.
What is the role of __new__ method in python?
The `__new__` method in Python is a special method that is called to create a new instance of a class before the `__init__` method is invoked
What is the role of __call__ method?
The `__call__` method in Python allows an object to be called like a function, enabling instances of a class to behave as callable objects.
What is the purpose of @property in python?
The `@property` decorator in Python is used to define a getter method for an attribute, allowing access to the attribute like a normal property.
What is the difference between class and object in python?
In Python, a class is a blueprint or template for creating objects, while an object is an instance of a class with its own unique identity and attributes.
What is Method chaining?
Method chaining in Python allows you to invoke multiple methods on an object in a single line by returning the modified object from each method, enabling concise and readable code.
What is the purpose of __next__ in python ?
The `__next__` method in Python iterators is used to retrieve the next element from an iterator, allowing you to iterate over a sequence of values using the `next()` function or a `for` loop.
What is the difference between shallow copy and view of a list?
A shallow copy of a list in Python creates a new list object with references to the original elements, while a view of a list provides a different way of looking at the same list without copying the elements.
What is '__del__' method in python ?
The `__del__` method in Python classes is a special method that is automatically called when an object is about to be destroyed or garbage collected, allowing you to perform cleanup actions or deallocate resources.
What is memoization?
Memoization in Python is a technique used to optimize the execution of functions by caching the results of expensive function calls and returning the cached result when the same inputs occur again, avoiding unnecessary computations.
What is the difference between dictionary and defaultdict?
A dictionary in Python is a collection of key-value pairs, while a defaultdict is a subclass of the dictionary that provides a default value when accessing a non-existing key, preventing key errors.
What is @classmethod?
The `@classmethod` decorator in Python is used to define a method that operates on the class itself rather than an instance of the class, allowing you to access class-level variables and perform class-specific operations.
What is __iter__ method?
The `__iter__` method in Python is used to define an iterator for an object, allowing it to be iterated over using the `iter()` function or a `for` loop, and returning an iterator object that provides the `__next__` method.
What is the difference between local and global variable in python?
A local variable in Python is defined within a function or a block and has a limited scope, while a global variable is defined outside of any function or block and is accessible throughout the entire program.
What is the significance of "pass" statement in python?
The `pass` statement in Python is a placeholder statement that does nothing, used as a syntactic requirement when a statement is needed for correct code structure but no action is required at that point.
What is List comprehension?
List comprehension in Python provides a concise way to create lists based on existing lists or other iterables, allowing you to filter, transform, and manipulate elements in a single line of code.
What is the significance of __enter__ and __exit__?
The `__enter__` and `__exit__` methods in Python context managers define the setup and teardown actions for a block of code using the `with` statement, allowing you to manage resources, exceptions, and cleanup operations.
What is decorator in python?
In Python, a decorator is a function that takes another function as input and extends its behavior without modifying its source code, providing a way to wrap or modify functions transparently.
What is enumerate()?
The `enumerate()` function in Python is used to iterate over a sequence while keeping track of the index and value of each element, returning an iterable of tuples containing the index and the element.
what is the significance of __len__ function?
The `__len__` method in Python is a special method that is called when the built-in `len()` function is used on an object, allowing you to define the length or size of an object.
What is duck typing?
Duck typing in Python is a programming concept that focuses on an object's behavior rather than its specific type, allowing different objects to be used interchangeably if they support the required methods or attributes.
What is regular expressions?
Regular expressions in Python are powerful tools for pattern matching and manipulation of strings, providing a concise and flexible way to search, extract, and replace specific patterns within text data.
What is the significance of locals()?
The `locals()` function in Python returns a dictionary containing the current local symbol table, providing access to all local variables and their values within the scope of the function or code block.
What is Async and Await keywords in python?
The `async` and `await` keywords in Python are used for asynchronous programming, allowing the creation of coroutines and the execution of concurrent tasks without blocking the main program's execution, improving efficiency and responsiveness.