Can We Have Two Classes with Same Name in Python?
The short answer: yes... but also no (it’s tricky)
Well, here's the thing — technically, you can have two classes with the same name in Python... but not in the same scope. Python will allow you to redefine a class by reassigning the name, but you’re not really having two classes at the same time. You're replacing one with the other.
Let me explain with a quick code snippet:
pythonclass Dog: def bark(self): print("Woof!")class Dog: def bark(self): print("WOOOF!!!")
Only the second Dog
class survives. The first one? Gone. Poof. Python just overwrote it like "okay, cool, new Dog now."
Digging deeper: how Python handles names
It's all about the namespace
Honestly, when I first realized this, I was a bit frustrated. I had this grand idea of dynamically loading modules that had their own class definitions — with the same names — and somehow magically coexisting. Yeah, nope.
Python stores names in something called a namespace. And when you define a class, that name goes into the current namespace. If you define it again — in the same scope — the old one is simply overwritten.
You can think of it like this:
“Hey Python, here’s a class called Dog
.”
Python: “Cool, got it.”
“Actually wait, here’s a new Dog
class.”
Python: “Alright, overwrite the old one. Moving on.”
So what if you really need two?
You gotta play with modules or dynamic imports. Let me show you something that saved me during a messy debugging night (shoutout to my teammate Clara who stayed on Zoom with me 'til 1am):
python# module_a.pyclass Dog: def bark(self): print("Woof from A")# module_b.pyclass Dog: def bark(self): print("Woof from B")# main.pyimport module_aimport module_ba_dog = module_a.Dog()b_dog = module_b.Dog()a_dog.bark() # Woof from Ab_dog.bark() # Woof from B
Boom. Two classes, same name, no conflict. Because they live in different modules, Python treats them like module_a.Dog
and module_b.Dog
. Separate identities. Peaceful cohabitation.
Why redefining a class can bite you
I’ve done it by accident more than once (especially in big Jupyter notebooks — ugh). You tweak a class down below, rerun a cell, and then some method behaves weirdly. Took me half an hour once to figure out my first class definition was silently replaced.
So yeah, be careful. Python won’t warn you. It’ll just shrug and say, “Sure, new version. Let’s roll.”
Tip: use clear naming if experimenting
If you're playing around or prototyping, do something like:
pythonclass DogV1: passclass DogV2: pass
It’s not fancy, but at least you’re not stepping on your own toes.
What about nested scopes? Can we cheat?
Now this is where it gets a little funky. You can have two classes with the same name — if they're defined in different scopes, like inside functions.
pythondef make_dog_class_a(): class Dog: def bark(self): print("Dog from A") return Dogdef make_dog_class_b(): class Dog: def bark(self): print("Dog from B") return DogDogA = make_dog_class_a()DogB = make_dog_class_b()DogA().bark() # Dog from ADogB().bark() # Dog from B
Is this clean? Not really. But it works. And when you’re dynamically generating or importing things, this kind of trick can be a lifesaver.
Final thoughts (and a little confession)
Actually, I used to think Python would refuse duplicate class names altogether. Back when I was learning, I remember telling my friend Eric, “No way, Python would throw a syntax error.” He laughed. Made me try it. I was wrong. It didn't throw anything.
So yeah, Python’s cool like that — flexible, but also a bit sneaky if you’re not paying attention. You can have two classes with the same name... as long as you respect the rules of scope. If not, well, expect some odd bugs (and probably a late night debugging session with coffee and regret).
My advice? Embrace modules. Be explicit. And always name your stuff like you're explaining it to your future self — tired, in a rush, and a little grumpy.
How much height should a boy have to look attractive?
Well, fellas, worry no more, because a new study has revealed 5ft 8in is the ideal height for a man. Dating app Badoo has revealed the most right-swiped heights based on their users aged 18 to 30.
Is 172 cm good for a man?
Yes it is. Average height of male in India is 166.3 cm (i.e. 5 ft 5.5 inches) while for female it is 152.6 cm (i.e. 5 ft) approximately. So, as far as your question is concerned, aforesaid height is above average in both cases.
Is 165 cm normal for a 15 year old?
The predicted height for a female, based on your parents heights, is 155 to 165cm. Most 15 year old girls are nearly done growing. I was too. It's a very normal height for a girl.
Is 160 cm too tall for a 12 year old?
How Tall Should a 12 Year Old Be? We can only speak to national average heights here in North America, whereby, a 12 year old girl would be between 137 cm to 162 cm tall (4-1/2 to 5-1/3 feet). A 12 year old boy should be between 137 cm to 160 cm tall (4-1/2 to 5-1/4 feet).
How tall is a average 15 year old?
Average Height to Weight for Teenage Boys - 13 to 20 Years
Male Teens: 13 - 20 Years) | ||
---|---|---|
14 Years | 112.0 lb. (50.8 kg) | 64.5" (163.8 cm) |
15 Years | 123.5 lb. (56.02 kg) | 67.0" (170.1 cm) |
16 Years | 134.0 lb. (60.78 kg) | 68.3" (173.4 cm) |
17 Years | 142.0 lb. (64.41 kg) | 69.0" (175.2 cm) |
How to get taller at 18?
Staying physically active is even more essential from childhood to grow and improve overall health. But taking it up even in adulthood can help you add a few inches to your height. Strength-building exercises, yoga, jumping rope, and biking all can help to increase your flexibility and grow a few inches taller.
Is 5.7 a good height for a 15 year old boy?
Generally speaking, the average height for 15 year olds girls is 62.9 inches (or 159.7 cm). On the other hand, teen boys at the age of 15 have a much higher average height, which is 67.0 inches (or 170.1 cm).
Can you grow between 16 and 18?
Most girls stop growing taller by age 14 or 15. However, after their early teenage growth spurt, boys continue gaining height at a gradual pace until around 18. Note that some kids will stop growing earlier and others may keep growing a year or two more.
Can you grow 1 cm after 17?
Even with a healthy diet, most people's height won't increase after age 18 to 20. The graph below shows the rate of growth from birth to age 20. As you can see, the growth lines fall to zero between ages 18 and 20 ( 7 , 8 ). The reason why your height stops increasing is your bones, specifically your growth plates.