对象方法调用是不可能的



在下面的代码中,我尝试使用多线程和同步的概念进行开发。我开发了下面的代码。但是,在运行时,我收到以下错误:

cls: <class '__main__.ThreadsWithSync'>
Traceback (most recent call last):
File "m:python lessonsThreadsWithSync.py", line 68, in <module>
t1.spawnThread
AttributeError: 'NoneType' object has no attribute 'spawnThread'
PS M:python lessons>   

请告诉我为什么不能识别'spawnThread'方法

进口线程导入日志导入的时间从随机导入种子从random导入randint

class ThreadsWithSync():
def __new__(cls):
"""
For object creation
"""
print("cls: %s"%(cls))
cls.onCreateObject()

def __init__(self):
"""
For object initialization
"""
#print("self: %s"%(self)) 
self.onInitializeObject()
@classmethod
def onCreateObject(cls):
"""
This will be invoked once the creation procedure of the object begins.
"""
instance = super(ThreadsWithSync, cls).__new__(cls)
#print("instace: %s"%(instance.__repr__)) #activate this line whenever an informative and descriprtive text about the instance is needed to be displayed
return instance
def onInitializeObject(self):
"""
This will be invoked once the initialization procedure of the object begins.
"""
threading.Thread.__init__(self) #to initialize the super class
print("self: %s"%(self))
seed(1)
def __repr__(self):
"""
similar to toString in Java
"""
return "n__class__: " + repr(self.__class__) +"n__new__:" + repr(self.__new__) + "n__str__: " + repr(self.__str__) + "n__sizeof__: " + repr(self.__sizeof__)

def isNumPrime(self, targetNum):
if targetNum == 0 or targetNum == 1:
return False
isPrim = True
for i in range(targetNum):
if targetNum % i == 0:
isPrim = False
break
return isPrim
def spawnThread(self):
if __name__ == "__main__":
main()
def main():
while True:
thread_1 = threading.Thread(group = None, target = self.isNumPrime, name='Thread_1', args = (), kwargs=dict(targetNum=randint(0,100)), daemon = None)
thread_2 = threading.Thread(group = None, target = self.isNumPrime, name='Thread_2', args = (), kwargs=dict(targetNum=randint(0,100)), daemon = None)

t1 = ThreadsWithSync()
t1.spawnThread

您忘记返回从__new__创建的对象