Skip to content Skip to sidebar Skip to footer

Initialize A Class?

I am trying to run a test but I continue to get the following error: --------------------------------------------------------------------------- TypeError

Solution 1:

The problem is that you're not creating a class instance. These lines are totally wrong:

a = AnimalShelter  # This is just an alias
a.__init__(a, username, password)  # treats the class like an instance of itself

What you want to do is this:

a = AnimalShelter(username, password)

This creates a new AnimalShelter instance and calls AnimalShelter.__init__() with the instance as the first parameter, self.


Note: As a rule of thumb, don't call __dunder__ methods manually. Most of them have entry points that add functionality, like for example, len() will validate the output of __len__(). That said, there are cases you'll need to call them directly, mostly when working with class internals, like for example, you'll often need to call super().__init__() when subclassing.

Post a Comment for "Initialize A Class?"