Typo That Broke My Python Class - My First Real Debugging Moment

Sagnik DevSagnik Dev
2 min read

Hi everyone! I started learning (and relearning?) Python and Object-Oriented Programming concepts a couple of days back, and while playing around with some simple class inheritance code, I ran into a weird bug that had me confused for a while.

So, the code is fairly simple; I wanted to create a few animal classes (Lion, Elephant, Monkey) that inherit from a base class called Animal. Each animal would have a name and species, and they could speak, and introduce themselves.

Here’s what I wrote:

Then I created child classes and objects:

When I ran the code, the speak() method worked just fine:

But the introduce() method crashed with the error:

Wait what?

Thus, the confusion. I thought I was setting self.name and self.species in the constructor. So why did Python say they didn’t exist?

After rereading the error and comparing my code to some examples online, I found it:

Yup, I had wriiten _init_ instead of __init__. Not putting double underscore, led to a faulty syntax as a result of which the Python Constructor was not recognised and thus not executed. The object was created without setting name and species. Python just executed the default object.__init__() behind the scenes-which did nothing.

As a result, only function speak() worked as it didn’t need any attributes. However function introduce() crashed when it tried to use attributes that were never created.

What I Learnt

This was definitely a small typo, but since I’m just starting out, gotta say it was a big learning moment. Methods that did not rely on attributes, still running on wrong syntax (_init_) was a key takeaway.

Thanks for reading- feel free to share your own beginner bugs or advice in comments!

0
Subscribe to my newsletter

Read articles from Sagnik Dev directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sagnik Dev
Sagnik Dev