2016-03-26 15 views
0

當我做扭曲使用callLater傳遞一個類的實例

reactor.callLater(5, my_func, self) 

這是傳遞給my_func,並將對象不是我叫使用callLater從自我。他們有不同的地址(通過print(self)和print(my_arg)驗證)。

我在這裏錯過了什麼?有沒有辦法讓 my_func(classObj) 看到發送的實際實例?

我的意圖是設置一個延遲檢查,以確定自己的某個屬性是否設置過。我可以以不同的方式做事(事實上已經有了),但有關情況的東西沒有正確的味道,所以我想我會澄清未來的應用。

回答

0

的確有些東西不聞起來沒錯。以下代碼按預期工作,傳入的變量具有相同的地址。

from twisted.internet import reactor 

def verify(obj): 
    """ Check whether the value variable has been set or not. 
    """ 
    if obj.value: 
     print('Value was set') 
    else: 
     print('Value was not set') 

def compare(A, B): 
    """ Compare obj A == B 
    """ 
    if A == B: 
     print('Objects are the same') 
    else: 
     print('Objects are not the same') 


class txClass: 
    value = None 

    def verify(self): 
     reactor.callLater(5, verify, self) 

    def compare(self, obj): 
     reactor.callLater(5, compare, self, obj) 


A = txClass() 
A.verify()   # verify obj.value is set 
A.value = 'test' 

B = txClass() 
A.compare(A)  # compare same obj 
A.compare(B)  # compare diff obj 
reactor.run()