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

浪子不回头ぞ 提交于 2019-12-05 18:04:05

问题


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


回答1:


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. :)




回答2:


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.




回答3:


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.




回答4:


Faced the same issue until I realized I had named the classes in both the files with the same name - pretty dumb!



来源:https://stackoverflow.com/questions/13094713/getting-an-attributeerror-class-has-no-attribute-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!