Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么要获取AttributeError:对象没有属性_Python - Fatal编程技术网

Python 为什么要获取AttributeError:对象没有属性

Python 为什么要获取AttributeError:对象没有属性,python,Python,我有一堂课要读。因为我有一个方法示例。我试图在同一对象上下文中运行它。请看一下代码: class myThread (threading.Thread): def __init__(self, threadID, name, counter, redisOpsObj): threading.Thread.__init__(self) self.threadID = threadID self.name = name self

我有一堂课要读。因为我有一个方法示例。我试图在同一对象上下文中运行它。请看一下代码:

class myThread (threading.Thread):
    def __init__(self, threadID, name, counter, redisOpsObj):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.counter = counter
        self.redisOpsObj = redisOpsObj

    def stop(self):
        self.kill_received = True

    def sample(self):
        print "Hello"

    def run(self):
        time.sleep(0.1)
        print "\n Starting " + self.name
        self.sample()
看起来很简单,不是吗。但是当我运行它时,我得到了这个错误

AttributeError:'myThread'对象没有属性'sample',现在我有了这个方法,就在那里。那怎么了?请帮忙

编辑:这是堆栈跟踪

Starting Thread-0

Starting Thread-1
Exception in thread Thread-0:
Traceback (most recent call last):
File "/usr/lib/python2.6/threading.py", line 525, in __bootstrap_inner
self.run()
File "./redisQueueProcessor.py", line 51, in run
self.sample()
AttributeError: 'myThread' object has no attribute 'sample'

Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.6/threading.py", line 525, in __bootstrap_inner
self.run()
File "./redisQueueProcessor.py", line 51, in run
self.sample()
AttributeError: 'myThread' object has no attribute 'sample'
我这样称呼它

arThreads = []
maxThreads = 2;

for i in range( maxThreads ):
    redisOpsObj = redisOps()
    arThreads.append( myThread(i, "Thread-"+str(i), 10, redisOpsObj) )

很抱歉,我无法发布redisOps类代码。但我可以向您保证,它工作得很好

您的缩进有问题,而且您混合了制表符和空格。使用python-tt运行脚本以进行验证。

在使用python多线程时,此类错误很常见。发生的是,在解释器被拆除时,相关模块myThread在本例中经历了一种delmythread

调用self.sample大致相当于myThread.\u dict\u[sample]self。
但是如果我们在解释器的分解序列中,那么它自己的已知类型字典可能已经删除了myThread,现在它基本上是一个非类型,并且没有“sample”属性。

Python通过内部更改名称以包含类名来保护这些成员。
您可以访问对象等属性。\u className\u attrName

如果您使用的是Python3+,那么如果您使用的是以双下划线开头的私有变量,例如self.\u yourvariable,也可能出现这种情况。对于可能遇到此问题的一些人,请注意。

您不能访问类的私有字段之外的内容。私有字段以\开头。 例如—

class car:
    def __init__(self):
        self.__updatesoftware()

    def drive(self):
        print("driving")

    def __updatesoftware(self):
        print("updating software:")

obj = car()
obj.drive()  
obj.__updatesoftware()  ## here it will throw an error because 

__updatesoftware是一种私有方法。

我在处理ZMQ时,在多线程场景中遇到了这个错误。结果表明,套接字仍然连接在一个线程上,而另一个线程已经开始发送数据。由于另一个线程试图访问尚未创建的变量而发生的事件。如果您的场景涉及多线程,并且如果您增加了一点延迟,那么您可能会遇到类似的问题。

我也遇到了相同的错误。我确信我的压痕没有任何问题。只有重新启动python服务器才能解决问题

当我使用另一个名为mythread的变量时,也发生了相同的错误。这个变量重写了这个,这就是我出错的原因。如果您在类中使用slot,并且还没有在slot中添加这个新属性,也可能会发生这种情况

class xyz(object):
"""
class description

"""

__slots__ = ['abc', 'ijk']

def __init__(self):
   self.abc = 1
   self.ijk = 2
   self.pqr = 6 # This will throw error 'AttributeError: <name_of_class_object> object has no attribute 'pqr'

你会在调用堆栈中发布完整的错误吗?你能添加你如何调用的代码吗?是否缺少一些代码。这个片段对我很有用。我非常抱歉。是的,我被骗了。完全是python新手。因此,您可能忽略了缩进的重要性。在编写Python代码时,您应该在代码编辑器中选择“显示制表符和空格”或“显示空格”以使人大开眼界。现在明白了。对不起,这个愚蠢的问题和对这样一个问题的解释太长了;请告诉我用python-tt运行脚本进行验证的意义是什么?@akshay_rahar:python-tt script.pySo,这个神奇的参数-tt做什么?我在更新中没有找到它:-tt标志在Python 3中不存在,它在Python 2中存在。作者没有试图访问私有方法,这个问题也已经解决了6年多了,因此不需要再次尝试回答它。您可以发布命令重新启动或执行任何操作。这可能对初学者有帮助。它是否记录在PIP中?只是偶然发现这个问题并发现了这个评论,为什么他们会这样设计?这里有记录: