← Back to list

This Weird Interview Question about Overwriting Python Classes!

During an interview, I was asked a question that made me pause and think about how Python is so flexible to a point that you can never be…

Sandeep Kumar · 2025-10-20 05:20 · 0 claps · 2.2 min read
#pydantic #python #oop #monkey-patching
Open on Medium ↗

This Weird Interview Question about Overwriting Python Classes!

During an interview, I was asked a question that made me pause and think about how Python is so flexible to a point that you can never be too carefull!

So, The Question goes like this

“What happens if you accidentally create an object with the exact same name as its class, somewhere else in your Python code?”

It all started when we were in a deep discussion about Pydantic and how I was using it for data validation and how it helped me enforce output structure for my Google ADK AI Agents, I told them, that for a scenario where I wanted to make the agent return a output metrics in a structure like below..

{
  "record_id": "196f8a63-98e8-4039-93e0-4e4b1080684b",
  "timestamp": "2025-10-19T22:13:00Z",
  "type": "support_ticket",
  "probability": 0.86,
  "confidence": 0.91
}

I told them I will define a class called Record extending Pydantic BaseModel class like below

from uuid import UUID
from datetime import datetime
from typing import Literal, float

class Record(BaseModel):
    record_id: UUID
    timestamp: datetime
    type: Literal["support_ticket", "bug_report", "feature_request"]
    probability: float
    confidence: float

And before I were to show the Agent snippet, I was asked a question what if there was another code that went like this,

sample_record = {
  "record_id": "196f8a63-98e8-4039-93e0-4e4b1080684b",
  "timestamp": "2025-10-19T22:13:00Z",
  "type": "support_ticket",
  "probability": 0.86,
  "confidence": 0.91
}

# creating an object with same name reference as the Class
Record = Record(**sample_record)

Alright, the very first thought that crossed my mind was No, Python would not allow this, but also there could be a case where it may allow given how flexible Python is..

So, I responded,

“I am afraid I do not know whether this is possible, but if it is, we may lose the reference to the class as result and

there would be no way of recovering the class reference”

The interviewer told me,

Yes, it is completely possible and again yes, we may lose the reference to the class itself but only the reference not the actual Class we created.

# at this point, this will show Record as an object of type class Record
print(type(Record))

And the interviewer asked me to take 5 mins to brainstorm different ways to recover the class and told me it is very simple and straightforward, so I just shot this first response at him with absolute confidence saying,

We could use the type function with the Record object as a parameter that which will return the type of Record Object which is type Record Class,

# currently the Record in the argument reference an object
Record = type(Record)

# we would see the type as a Pydantic class
# thus we recovered the Class reference
print(type(Record))

So, he responded that my approach is correct and told me that this scenario may be rare but rather a very important concept called Monkey Patching and laughed how I fell victim for this question to him,

Later he quoted,

How Python is so flexible to a point that you can never be too careful!

Regards, Sandeep Kumar

*https://www.linkedin.com/in/sandeepkumar-v/*


메타데이터
post_id
b69c86b7d0f4
slug
this-weird-interview-question-about-overwriting-python-classes-b69c86b7d0f4
url
https://medium.com/@sandeepkumarvsk10/this-weird-interview-question-about-overwriting-python-classes-b69c86b7d0f4
canonical_url
https://medium.com/@sandeepkumarvsk10/this-weird-interview-question-about-overwriting-python-classes-b69c86b7d0f4
author_url
https://medium.com/@sandeepkumarvsk10
status
ok
fetched_at
2026-06-21 15:33:18