Tag: 属性错误

AttributeError(“'str'对象没有属性'read'”)

在Python中,我收到一个错误: Exception: (<type 'exceptions.AttributeError'>, AttributeError("'str' object has no attribute 'read'",), <traceback object at 0x1543ab8>) 鉴于python代码: def getEntries (self, sub): url = 'http://www.reddit.com/' if (sub != ''): url += 'r/' + sub request = urllib2.Request (url + '.json', None, {'User-Agent' : 'Reddit desktop client by /user/RobinJ1995/'}) response = urllib2.urlopen (request) jsonofabitch = response.read () return json.load […]

__getattr__在模块上

如何在一个模块上实现相当于__getattr__的类? 例 当调用模块的静态定义的属性中不存在的函数时,我希望在该模块中创build一个类的实例,并在模块上的属性查找中调用其名称与失败相同的方法。 class A(object): def salutation(self, accusative): print "hello", accusative # note this function is intentionally on the module, and not the class above def __getattr__(mod, name): return getattr(A(), name) if __name__ == "__main__": # i hope here to have my __getattr__ function above invoked, since # salutation does not exist in the current […]

AttributeError:'模块'对象没有属性

我有两个python模块: a.py import b def hello(): print "hello" print "a.py" print hello() print b.hi() b.py import a def hi(): print "hi" 当我运行a.py ,我得到: AttributeError: 'module' object has no attribute 'hi' 错误是什么意思? 我如何解决它?

Python:属性错误 – 'NoneType'对象没有属性'something'

我不断收到一个错误,说 AttributeError: 'NoneType' object has no attribute 'something' 我的代码太长了,不能在这里发布,但是我想知道是否有人可以给出一般情况会导致这个'AttributeError', 'NoneType'应该是什么意思? (通常你会收到一些代码出错的对象的名称,但是由于它给了我'NoneType',我不确定如何缩小它的行为,除了第#行以外)