2

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
4

1 回答 1

3

在类定义中声明但不在方法中声明的每个变量都是类变量。所以如果你想要一个“对象变量(一个实例变量)”,你必须在方法中声明它:

    class MyClass:
       def something(self):
          self.variable_for_the_object = 1

这个答案有更多关于它的细节:https ://stackoverflow.com/a/69067/1399290

于 2013-11-03T15:16:00.403 回答