获取 AttributeError: <class>没有属性&lt;method&gt; [英] Getting an AttributeError: &lt;class&gt; has no attribute &lt;method&gt;

查看:31
本文介绍了获取 AttributeError: <class>没有属性&lt;method&gt;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在模块 mod1 的类中创建一个方法并按如下方式调用它:

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

并将其称为:

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

它抛出一个属性错误:blahblah 没有名为 foobar 的属性

你能帮我吗?提前致谢

推荐答案

您描述的错误类型可能只是由不匹配的缩进引起的.如果方法在类的最底层,将其在类中向上移动一点,问题就会变得明显.

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.

当 python 解释器遇到不匹配的缩进时(比如你开始在一个用空格缩进的文件底部使用制表符),解释器不会总是抛出错误;它可以简单地忽略文件的其余部分.就在今天,我在更新一些旧代码时遇到了这个问题,其中原作者使用了不同的空白字符(恰好与我的 Geany 选项卡匹配),它让我循环了比我想承认的更长的时间.:)

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

这篇关于获取 AttributeError: <class>没有属性&lt;method&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆