Python Data Science Jobs & Interviews
20.4K subscribers
188 photos
4 videos
25 files
326 links
Your go-to hub for Python and Data Science—featuring questions, answers, quizzes, and interview tips to sharpen your skills and boost your career in the data-driven world.

Admin: @Hussein_Sheikho
Download Telegram
⁉️ Interview question
In Python, what happens when a class inherits from multiple classes that have a method with the same name, and how does the Method Resolution Order (MRO) determine which method gets called?

Simpson:
When a class inherits from multiple parent classes with a method of the same name, Python uses the **Method Resolution Order (MRO)** to decide which method is invoked. The MRO follows the **C3 linearization algorithm**, which ensures a consistent and deterministic order based on the inheritance hierarchy. This means that if you call the method, Python traverses the classes in a specific sequence defined by the MRO, starting from the child class and moving through parents in a depth-first, left-to-right order. If a method is found in one of the parent classes before others, it will be used, even if other parents also define the same method. The MRO can be inspected using `ClassName.mro()` or `help(ClassName)`. However, if there’s an ambiguity in the inheritance structure—such as a diamond pattern without proper resolution—the C3 algorithm still resolves it, but unexpected behavior may occur if not carefully designed. This makes understanding MRO crucial for complex inheritance scenarios.

#️⃣ tags: #Python #AdvancedPython #Inheritance #MethodResolutionOrder #MRO #OOP #ObjectOrientedProgramming #InterviewQuestion

By: t.iss.one/DataScienceQ 🚀