Python interview Questions — Advanced

chanduthedev
3 min readSep 17, 2023

--

  1. what is constructor? How to create constructor in python?
  • A constructor is a special method in a class and it is used to initialize an object for a class
  • Normally in most of the languages, constructor and class name should be same, but not in python
  • __init__ is the constructor name in python class

2. Will python support multiple inheritance?

  • Python will support multiple inheritance
  • If more than one parent class has same method name, python will follow MRO approach
  • MRO is Method Resolution Order
  • MRO follows certain principles to find MRO of the function
    - MRO searches for sub classes before going to the parent classes
    - MRO will searches for methods from left to right order
  • class_name.__mro__ will display MRO as a list for the class

3. What is class method, object method and static method?

  • Class method:
    - Is used to access methods using class name
    - Class method will have access to all the data within the class.
    - Need to use classmethod decorator to make a method as classmethod
    - Class method always takes first argument as cls keyword
  • Object method:
    - Object method used to access methods using object name
    - To access object method, method always need a object for a class
    - By default, all methods in the class are object methods
    - Object method always takes first argument as self keyword
    - Object method is also called instance method
    - Object methods are used to update/modify the data with in the class
  • Static method:
    - Static methods are similar to Class methods, but Static methods are not bound to Class
    - Static methods should not access/modify class data
    - Need to use staticmethod decorator to make a method as static method
    - Static method should be created when some functionality is not part of the object, part of whole class

4. What is abstract class?

  • Abstraction is a concept in object oriented programming
  • Abstraction is basically hiding internally implementation
  • Python Support this abstraction using module called abc(Abstract Base Class)
  • Need to use abstractmethod decorator to make a method as abstract method
  • Abstract method should not have implementation in the abstract class, derived class must and should implement the method

5. What is memoization?

  • Memoization is a concept of storing values and retrieved when required from the stored location.
  • This will save lot of processing time and eliminates repeated operations.
  • This concept really helpful in recursive operations and avoid memory overflow exceptions.

6. What are magic methods or dunder methods?

  • Magic methods are special functions whose names start and ends with double underscore(__)
  • These magic methods are also called dunder methods as their name start and ends with double underscore(__)
  • One of the best example for dunder method is __init__ constructor method for a class
  • Python class by default provides many dunder methods like __str__, __le__, __ge__, __dict__, __doc__, __repr__ etc
  • Get all the supported dunder function for a class by calling dir(class_name))

7. What is Singleton?

  • Singleton is one of the popular design pattern
  • This will allow to create only one instance of a class in the entire application
  • application launching instance, logging are some of the examples of singleton pattern
  • To create a singleton class
    - Add class variable with null/None value to the class
    - Create a static method like get_instance or similar name
    - Implement static method in such way that, if class variable has value, return static variable otherwise create a new instance of the class and assign that instance to the static variable
  • Check below sample code for the reference

8. What is GIL?

  • GIL is Global Interpretor Lock
  • GIL is a kind of lock(mutex) which allows only one thread execution at a time
  • GIL will make sure that execution is thread safe
  • Python will not support multi threading, but it looks like multi threading, but in reality only one thread will execute due to GIL

9. What is multi threading? how multi threading works in python?

  • Multi threading is a process of creating multiple thread in single application or program
  • Python doesn’t support multi threading due to GIL, But python execution looks like multi threading, but in the execution it is concurrency.
  • Python supports Multi processing in which multiple processes run simultaneously and each process doesn’t share any resources

--

--

No responses yet