Python Interview Trick: Binding Methods and Static Methods

💡 Interview Insight: A Small Python Trick That Can Trip You Up This question came up in one of my interviews: class Math1: def add(self, *abcd) -> int: sum = 0 for val in abcd: sum += val return sum print(Math1.add(1, 2)) 👉 What will be the output? Most people instinctively say 3… but the correct answer is: ➡️ 2 --- 🧠 Why? When calling: Math1.add(1, 2) Python binds arguments like this: - "self = 1" - "abcd = (2,)" So internally it becomes: sum = 0 sum += 2 👉 Final result = 2 --- ⚠️ Key Learning - Python does not enforce "self" to be an instance - Methods are just functions until properly bound - This works only because "self" is not used --- ✅ Best Practice If no instance data is needed: class Math1: @staticmethod def add(*abcd): return sum(abcd) Or call it properly: Math1().add(1, 2) # 3 --- 🚀 Takeaway Understanding how Python binds methods is more important than just knowing syntax. This is the kind of subtle concept interviewers use to differentiate between: - surface-level knowledge ❌ - deep understanding ✅ --- Have you seen similar tricky questions in interviews? Drop them below 👇 #Python #CodingInterview #BackendDevelopment #SoftwareEngineering #TechCareers #Learning

To view or add a comment, sign in

Explore content categories