蟒蛇。我是否正确理解了代码?

时间:2015-05-12 01:45:49

标签: python

我有一个小问题。这段代码是否意味着:"每当MyThread类的实例创建时,初始化threading.Thread构造函数并将传递的参数分配给MyThread类中的变量"。本质上这个类的作用是创建一个threading实例.Thread类AND添加了一些自定义功能,比如新变量。正确?

class MyThread(threading.Thread):
      def __init__(self, func, args, name=''):
        threading.Thread.__init__(self)
        self.name = name
        self.func = func
        self.args = args

如果我是对的,这段代码

class MyThread(threading.Thread):
          def __init__(self):
            threading.Thread.__init__(self)

只需创建一个threading.Thread类的实例,实际上只需添加a = threading.Thread()即可。正确的吗?

2 个答案:

答案 0 :(得分:1)

class MyCls(BaseCls):
    def __init__(self):
        BaseCls.__init__(self)

相同
class MyCls(BaseCls):
    pass # constructor not overriden
在创建BaseCls个对象时,将在两种情况下调用

MyCls构造函数。

MyCls(当“空”时)和BaseCls仍有不同之处时,如果例如BaseCls使用__slots__,则会有一些差异。

答案 1 :(得分:0)

该部分代码未创建threading.Thread的实例。代码声明了一个类threading.Thread()的子类(或特化),也就是说,当您创建MyThread的实例时,它本身就是threading.Threadthreading.Thread.__init__(self)只是通过调用类构造函数将类实例初始化为threading.Thread,这通常在使用新样式类时使用super(MyThread, self).__init__()

执行a = threading.Thread()只是创建Thread类的实例,仅此而已。

相关问题