0% found this document useful (0 votes)
22 views3 pages

Advanced Python Interview Insights

Uploaded by

pavanbarma777
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views3 pages

Advanced Python Interview Insights

Uploaded by

pavanbarma777
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Advanced Python Interview Questions (4+ Years)

1. What is the difference between deepcopy() and copy()?

copy() creates a shallow copy, whereas deepcopy() creates a deep copy. Shallow copy copies references of

nested objects; deep copy duplicates them.

Example:

import copy

lst = [[1, 2], [3, 4]]

shallow = [Link](lst)

deep = [Link](lst)

lst[0][0] = 100

# shallow reflects the change, deep does not.

2. How does Python?s garbage collector handle circular references?

Python uses reference counting and a generational garbage collector to collect cyclic references. The gc

module can detect and collect circular references.

3. How can you make a custom object hashable and sortable?

Implement __hash__ and __eq__ for hashable objects, and __lt__ or other comparison methods for sorting.

Example:

class Person:

def __init__(self, name): [Link] = name

def __hash__(self): return hash([Link])

def __eq__(self, other): return [Link] == [Link]

def __lt__(self, other): return [Link] < [Link]

4. Difference between staticmethod, classmethod, and instance methods?

- instance method: first argument is self

- classmethod: first argument is cls

- staticmethod: no implicit arguments

5. Why doesn?t Python?s multithreading achieve true parallelism?


Advanced Python Interview Questions (4+ Years)

Because of the Global Interpreter Lock (GIL), only one thread executes Python bytecode at a time. For true

parallelism, use multiprocessing.

6. How do generators work and why are they memory efficient?

Generators yield values one by one using the 'yield' keyword. They don?t store all values in memory, making

them suitable for large data streams.

7. What are metaclasses in Python?

A metaclass is a class of a class. It controls the creation and behavior of classes. Example use case: ORM

field validation in Django.

8. How does async/await work in Python?

They enable asynchronous programming. Python uses an event loop to run async functions. Libraries like

asyncio are built on top of it.

9. What is a context manager and how do you create one?

Context managers manage resources. You can create one using a class with __enter__ and __exit__

methods or using @[Link].

10. Explain the difference between is and ==.

'is' checks for identity (same object in memory), '==' checks for equality in value.

11. How do you create a plugin system in Python?

Use dynamic imports (importlib), entry points, or abstract base classes. Useful for extensible frameworks.

12. What is the difference between @property and a regular method?

@property allows you to access a method like an attribute. It?s used for controlled attribute access.

13. What are *args and **kwargs used for?

They allow passing variable number of arguments to a function. *args for non-keyworded, **kwargs for

keyworded arguments.

14. How do you prevent race conditions in Python?


Advanced Python Interview Questions (4+ Years)

Use thread synchronization methods like [Link] or use multiprocessing for CPU-bound tasks.

15. What is the difference between mutable and immutable types in Python?

Mutable objects can be changed in-place (e.g., list, dict); immutable cannot (e.g., int, str, tuple). Tuples can

contain mutable elements.

Common questions

Powered by AI

Async/await syntax facilitates asynchronous programming by allowing functions to be paused and resumed, making non-blocking operations possible. Python runs these async functions on an event loop, enabling concurrency without using traditional threading models. Libraries like asyncio use this functionality to manage I/O-bound tasks more efficiently .

Python's multithreading does not achieve true parallelism due to the Global Interpreter Lock (GIL), which restricts bytecode execution to one thread at a time. To achieve true parallelism, Python offers multiprocessing, which allows parallel execution by running separate processes with their own memory space .

A plugin system can be implemented in Python using dynamic imports with importlib, defining entry points, or utilizing abstract base classes for plugin architecture. This allows extensible frameworks where additional functionality can be plugged into the system dynamically, enhancing flexibility and modularity .

deepcopy() creates a deep copy of an object, meaning all nested objects are duplicated independently. In contrast, copy() creates a shallow copy where nested objects are not duplicated, meaning they still reference the same memory as the original objects .

The 'is' operator checks for object identity, meaning it returns True if both operands refer to the same memory location. In contrast, '==' checks for value equality between objects. 'is' is primarily used for checking singleton objects like None, while '==' is used to compare the values of objects .

Metaclasses are the classes of classes, controlling class creation and behavior. They allow customization of class instantiation and are commonly used for frameworks like ORM (Object Relational Mappers) in Django, where they can enforce field validation rules and database schema control .

Generators are memory efficient because they yield values one at a time using the 'yield' keyword instead of storing all values at once. This allows them to handle large data streams with minimal memory usage compared to regular functions that return lists or other data structures with all elements at once .

Python manages circular references using a combination of reference counting and a generational garbage collector to track and collect cyclic references. The gc module plays a crucial role in detecting these circular references and facilitating their collection when reference counting alone is insufficient .

To make a custom object hashable, you need to implement the __hash__ and __eq__ methods to dictate how object hashes are generated and how equality is determined. For sorting, you must implement comparison methods like __lt__. For instance, in a Person class, __hash__ can return a hash of a name attribute, __eq__ compares names for equality, and __lt__ defines sorting order .

Context managers manage resources by ensuring that setup and cleanup activities are executed timely, often using the __enter__ and __exit__ methods. They are typically created using a class with these methods or with the @contextlib.contextmanager decorator, allowing the management of resources like file or network connections with minimal effort .

You might also like