Python类覆盖数据我正在使用线程在python中创建任务管理器,但是当我实例化两个任务时,第二个任务的方法会覆盖第一个任务的方法.我不希望这样课堂任务import timeimport sysfrom threading import Threadclass Ta...

Python类覆盖数据
我正在使用线程在python中创建任务管理器,但是当我实例化两个任务时,第二个任务的方法会覆盖第一个任务的方法.我不希望这样
课堂任务
import time
import sys
from threading import Thread
class Task(Thread):
name = ""
timeout = 300
data = {}
debug = False
"""
Config.type -> repeat, once, forever
repeat : if return True on call
once : repeat only one time
forever : repeat for time undefined
"""
task_config = {
"type": "repeat",
"time": 5,
"call": lambda: False,
"limit_call": False,
"getDataFrom": ()
}
def __init__(self, name, **options):
Thread.__init__(self)
self.name = name
self.task_config.update(options)
def set_debug(self, active):
self.debug = active
def set_config(self, options):
print "================================================"
print self.name
print "Before Changes"
print options
print self.task_config
self.task_config.update(options)
print "After Changes"
print self.task_config
def run(self):
method = self.task_config['call']
sleep_time = self.task_config['time']
print method
if not hasattr(method, '__call__'):
raise TypeError('<TaskManager> config.call isn\'t a function')
response = True
while response:
if self.debug:
sys.stdout.write("Call: " + self.name)
response = method.__call__(*self.data)
sys.stdout.flush()
time.sleep(sleep_time)
def test():
print 324342
return False
def test2():
print "Test 2 called\n"
return False
manager = TaskManager()
task = manager.create_task("Trhead 1\n", call=test, time=2)
task2 = manager.create_task("Trhead 2\n", call=test2, time=1)
task.start()
task2.start()
输出量
================================================
Name: Trhead 1
Before Changes
Option: {'call': <function test at 0x0000000001F509E8>, 'time': 2}
Class Config: {'call': <function <lambda> at 0x0000000001F50C18>, 'getDataFrom': (), 'type': 'repeat', 'limit_call': False, 'time': 5}
After Changes
Class Config: {'call': <function test at 0x0000000001F509E8>, 'limit_call': False, 'time': 2, 'type': 'repeat', 'getDataFrom': ()}
================================================
Name: Trhead 2
Before Changes
Option: {'call': <function test2 at 0x0000000001F50E48>, 'time': 1}
Class Config: {'call': <function test at 0x0000000001F509E8>, 'limit_call': False, 'time': 2, 'type': 'repeat', 'getDataFrom': ()}
After Changes
Class Config: {'call': <function test2 at 0x0000000001F50E48>, 'limit_call': False, 'time': 1, 'type': 'repeat', 'getDataFrom': ()}
<function test2 at 0x0000000001F50E48>
Test 2 called
解决方法:
在类定义内而不是在方法内声明的每个变量都是类变量.因此,如果要“对象变量(实例变量)”,则必须在方法内部声明它:
class MyClass:
def something(self):
self.variable_for_the_object = 1
该答案有更多详细信息:https://stackoverflow.com/a/69067/1399290
沃梦达教程
本文标题为:Python类使用相同的内存


基础教程推荐
猜你喜欢
- Shell命令从python失败,从shell正常 2023-11-12
- python数据结构之面向对象 2023-08-09
- Python爬取几千条相亲文案 2023-08-04
- 13行python代码实现对微信进行推送消息的示例代码 2022-08-30
- python开发之virtualenv与virtualenvwrapper(linux下安装与配置) 2023-09-04
- Pytorch关于Dataset 的数据处理 2023-08-04
- Linux软件:Anaconda安装python和自带Python 2023-09-03
- CentOS7.2下安装python安装pip-8.0.2管理 2023-09-04
- 通过OpenCV实现对指定颜色的物体追踪 2023-08-11
- python中not not x 与bool(x) 的区别 2023-08-11