Python interview Questions — Intermediate level
4 min readSep 13, 2023
- What is decorator?
- In General, decorator is a design pattern, In python decorator pattern is very useful
- A decorator pattern helps to add new functionality to existing function without effecting actual functionality
- ‘@’ character used to represent decorator in python
- To apply decorator functionality, just add decorator name with ‘@’ just before the function name.
- See below for decorator example with and without arguments
2. What is generator?
- A Generator is a function which returns an iterator using the yield keyword.
- In simple terms, A generator is a function which use yield keyword instead of return statement
- A Python generator function allows you to declare a function that behaves like an iterator
- Generator will stop the execution and returns when reaches yield statement and stores the state and resumes when called again, this continues like a iterator until it doesn’t find next element.
- Generator can be used to generate infinite series
3. What is the difference between function and generator?
- Main difference between function and generator is return statement
- Function will use return statement and generator will use yield keyword
- Function will return after completing the logic and it wont store the state
- Generator will return when reaching yield state and store the state and when generator calling next time, it will continue from the stored state and proceed until it reaches yield statement
- Generator is a iterator, show it should called in loop or iterative way.
- Generators are memory efficient
- See below image for sample code for function and generator
4. When to use function and when to use generator?
- Generators are memory efficient
- A normal function will find sequence of items, store them in memory and return final result where as generator will return one result when reaching yield
- Function should be used when whole results wants in one call and generators should be used when sequence of items needed one by one.
- Some use cases for generators are infinite sequence generation, reading large file line by line
5. what are default arguments?
- Default arguments are arguments in the function with default values, which means, default value will be used if no value passed
- Default arguments represented as arg_name=value in the function
- Default arguments are evaluated only once when the function is defined, we have to be careful when using mutable objects(List, Set, Dictionary) as default arguments
- Always pass normal arguments first followed by default arguments
6. What are the pitfalls when passing list/map as default parameter in python?
- As default arguments evaluated at the time of function definition
- If list/map as passed as default argument, these are mutable objects, exiting object will be updated instead creating a new object from second call on wards
- Its always better to avoid using list/set/map as default arguments, if passed always check for empty check and if the value is not empty create new empty object and proceed to avoid inconsistency
7. What is context manager in python?
- Context manager is easy way to maintain resources.
- It will be used with ‘with’ keyword
- It will eliminate boilerplate code
- Context manager internally uses __enter__ and __exit__ functions
- Best use case for context manage is operations on files. If we use context manager, no need to close the file after opening it. Context manager will close automatically once its coming out the scope
8. What is deep copy and shallow copy?
- Normally equal sign(=) is used to create a copy, however using =, it will only create reference and it will not create new object
- When we modify one reference it will be visible in another reference as both are using same reference/id/address when using equal sign(=)
- Shallow copy is just incrementing reference count and not creating a new object when copying/modifying
- Deep copy is creating a separate new object when copying and modifying one object will not effect other object
- copy module is used to make deep copy
9. What is the difference between Name error and Attribute error?
- NameError will raise when undefined local or global variable found
- AttributeError will raise when reference method not present in the object
- In the custom class, if we try to call a method which not present in the class will through AttributeError exception
- In the below example string object doesn’t have append method