Skip to main content
 首页 » 编程设计

Python。我是否正确理解了代码

2025年05月04日10xxx_UU

我有一个小问题。这段代码是否意味着:“每当创建 MyThread 类的实例时,初始化 threading.Thread 构造函数并将传递的参数分配给 MyThread 类内的变量”。本质上,此类所做的是创建 threading.Thread 类的实例并添加一些自定义功能,例如新变量。对吧?

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() 来完成。正确吗?

请您参考如下方法:

是的。

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

与

相同
class MyCls(BaseCls): 
    pass # constructor not overriden 

BaseCls 构造函数将在创建 MyCls 对象时在两种情况下调用。

MyCls(当“空”时)和 BaseCls 仍然是不同的东西,如果例如 BaseCls 使用 ,就会有一些区别>__插槽__。