Getting an AttributeError: <class> has no attribute <method>

Issue

I am creating a method in a class in a module mod1 and calling it as follows:

class blahblah:
   def foobar(self, bvar, **dvar)
       ////
       return dvar

And calling it as:

obj1 = mod1.blahblah()
dvar1 = obj1.foobar(True, **somedictionary)

It throws a Attribute error: blahblah has no attribute named foobar

Could you please help me with it? Thanks in advance

Solution

The type of error you describe can be caused simply by mismatched indentation. If the method is at the very bottom of your class, move it up in the class a bit and the problem will become apparent.

When python interpreters run into mismatched indents (like say you started using tabs at the bottom of a file that was indented with spaces), the interpreter will not always throw an error; it can simply ignore the rest of the file. I ran into this just today while updating some old code where the original author used different whitespace chars (that happened to match my Geany tabs), and it threw me for a loop for a lot longer than I’d like to admit. 🙂

Answered By – 3vi1

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published