python引用传递产生的问题_python关于lambda、引用传递等易犯的错误
[Python] 純文本查看 復(fù)制代碼import time
import threading
def test(i,count, _type):
time.sleep(3)
if lock.acquire():
if type(count) == int:
count += 1
else:
count[0] += 1
lock.release()
print(_type, ":", i, count)
if __name__ == '__main__':
threads = []
lock = threading.Lock()
# 情況一
cnt = 0
for i in range(3):
threads.append(threading.Thread(target=test, args=(i,cnt, "type 1")))
# 情況二
cnt2 = [0]
for j in range(3,6):
threads.append(threading.Thread(target=lambda: test(j, cnt2, "type 2")))
for t in threads:
t.daemon = True
t.start()
for t in threads:
t.join()
# 這是幾
print(cnt)
# 這又是幾
print(cnt2)
input("end!")
總結(jié)
以上是生活随笔為你收集整理的python引用传递产生的问题_python关于lambda、引用传递等易犯的错误的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 中望cad插件_中望软件与狄诺尼达成战略
- 下一篇: 12123选牌漏洞_12123选牌漏洞是