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
8 Answers 8
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. :)
3 Comments
When I ran into this problem, I immediately started checking for unbalanced indents, tabs, etc... Everything seemed correct but the error continued to appear. I walked away, came back, took another look, and DUH..., I found that I had a typo. instead of __init__(), I had typed __inti__(). So check all of your constructor's syntax first.
1 Comment
I had the same issue, and for me it happened when I moved the class file, but I left a .pyo file in the old folder, and python was still reading that .pyo file instead of reading the moved .py file.
Comments
Very old question, but I quote @Jacquot 's comment since it solved my problem (I was using %autoreload in ipython).
For what it's worth, it can also happen when using the %autoreload magic command in jupyter notebook, when you modify some methods in your module code (ipython.org/ipython-doc/3/config/extensions/...)
In particular, I solved the problem re-running the cell that was importing my class.
Comments
Old question, but for those who are facing this problem and none of the other answers could help you, this could be helpful. I was using Pickle to save an entire class with some data inside it, and loading this class instance again, but I had added some class methods and attributes on init, that was why the interpreter couldn't find the new attributes described inside my class (It was loading the "old" class within the pickle object)
1 Comment
Faced the same issue until I realized I had named the classes in both the files with the same name - pretty dumb!
3 Comments
For Jupyter notebooks using VSCode, what worked for me is to restart VSCode after changes to the imported file.
Comments
In my case, I just added ClassName to the method call, and it started working:
Wrong:
import Clases.Class_filename as LWD
articles=LWD.method_name(parameters)
Corrected:
import Clases.ClassName as LWD
articles=LWD.ClassName.method_name(parameters)
And the Clases/Class_filename.py contains something like this:
class ClassName :
def method_name(parameters):
....
foobarasfubar, etc)?foobaris not a method defined insideblahblah. Since you assert that it is defined as such, you should not be seeing this error. Are you sure thatfoobaris defined insideblahblahin your real code?blahblahtwice? Once withfoobardefined, and once without? (i.e. overriding your previous definition)dir(mod1.blahblah)(isfoobarthere?),dir(obj1)(isfoobarthere?),obj1.__class__(is itmod1.blahblah?),obj1.__module__(is itmod1?, etc.