kateli1991

python float对象没有属性“value”



我有4个共享变量。我正在更新一对基于哪个进程跑步。跟着是密码。在

class App(multiprocessing.Process):
    def __init__ (self,process_id):
        multiprocessing.Process.__init__(self)
        self.process_id = process_id
   def run(self,X,Y,lock):
        while True: 
               with lock :
               #some calculations which returns x and y 
               print 'x and y returned are :',x,y
                 try:
                    X.value = x
                    Y.value = y
                 except Exception ,e:
                   print e



if __name__ == '__main__':
    xL = Value('d',0.0)
    xR = Value('d',0.0)
    yL = Value('d',0.0)
    yR = Value('d',0.0)

    lock = Lock()

    a = A('1')
    b = B('2')

    process_a = Process(target = a.run, args(xL,yL,lock, ))  
    process_b = Process(target = b.run, args(xR,yR,lock, ))

    process_a.start()
    process_b.start()

这是输出:

x and y returned are : 375 402

'float' object has no attribute 'value'

有什么帮助吗?在


已被浏览了9072次
1 周,3 日 之前提问
1 个回答
fefe Tyson

看看我用括号中的数字注释的代码的三个部分:

   def run(self,X,Y,lock):
        while True:
               #some calculations which returns x and y 
               with lock :
                 X.value = x
                 Y.value = y

                 # (3) you now try to access an attribute of the arguments
                 # called 'value'
                 print X.value , Y.value , self.process_id


if __name__ == '__main__':
    xL = Value('d',0.0) # (1) these variables are assigned some objects that
    xR = Value('d',0.0) # are returned by the function 'Value'
    yL = Value('d',0.0) 
    yR = Value('d',0.0) 

    lock = Lock()

    a = A('1')
    b = B('2')

    # (2) now your variables are being passed to the 'a.run' and 'b.run'
    # methods
    process_a = Process(target = a.run, args(xL,yL,lock, ))  
    process_b = Process(target = b.run, args(xR,yR,lock, ))

    process_a.start()
    process_b.start()

当您跟踪代码的执行时,可以看到它正试图访问函数Value返回的任何对象中名为value的属性。您的错误消息告诉您,Value正在返回float对象,这些对象没有value属性。在

以下情况之一(最有可能)对您不利:

  1. 函数Value并没有按照您所想的那样做,它在不该做的时候返回float
  2. 在“一些返回x和y的计算”代码中,使用浮点重写x和y参数。在
评论 - 2020年7月29日 10:44

最新Python问答

推荐Python问答