Skip to main content
 首页 » 编程设计

python之为什么父类要在 Python 中使用子类的实例

2025年05月04日8mayingbao

multiprocessing 包中的process.py 的源代码使用以下模式;注意Process、_MainProcess和_current_process之间的关系。

class Process(object):                                                                                                                                                                                                                                                      
    '''                                                                                                                                                                                                                                                                     
    Process objects represent activity that is run in a separate process                                                                                                                                                                                                    
 
    The class is analagous to `threading.Thread`                                                                                                                                                                                                                            
    '''                                                                                                                                                                                                                                                                     
    _Popen = None                                                                                                                                                                                                                                                           
 
    def __init__(self, group=None, target=None, name=None, args=(), kwargs={}):                                                                                                                                                                                             
        assert group is None, 'group argument must be None for now'                                                                                                                                                                                                         
        count = _current_process._counter.next()                                                                                                                                                                                                                            
        self._identity = _current_process._identity + (count,)    

其中 _current_process 稍后定义为:

class _MainProcess(Process):                                                                                                                                                                                                                                                
 
    def __init__(self):                                                                                                                                                                                                                                                     
        self._identity = ()                                                                                                                                                                                                                                                 
        self._daemonic = False                                                                                                                                                                                                                                              
        self._name = 'MainProcess'                                                                                                                                                                                                                                          
        self._parent_pid = None                                                                                                                                                                                                                                             
        self._popen = None                                                                                                                                                                                                                                                  
        self._counter = itertools.count(1)                                                                                                                                                                                                                                  
        self._children = set()                                                                                                                                                                                                                                              
        self._authkey = AuthenticationString(os.urandom(32))                                                                                                                                                                                                                
        self._tempdir = None                                                                                                                                                                                                                                                
 
_current_process = _MainProcess()                                                                                                                                                                                                                                           
del _MainProcess      

我不明白的是 _current_process 是 _MainProcess 类的唯一实例,它是 Process 的子类,其中使用了 _current_process。这对我来说似乎是循环的。

  1. 我怀疑它能否用 C++ 或 JAVA 编译; Python 如何处理这个问题?
  2. 此外,这对我来说似乎是一种不寻常的模式;它有什么用?

上面的代码在下面的例子中得到了简化,它运行良好并打印出 42。

"""                                                                                                                                                                                                                                                                         
parent class definition use a instance of subclass???                                                                                                                                                                                                                       
"""                                                                                                                                                                                                                                                                         
 
 
class A(object):                                                                                                                                                                                                                                                            
 
    def print_num(self):                                                                                                                                                                                                                                                    
        print b.num                                                                                                                                                                                                                                                         
 
 
class B(A):                                                                                                                                                                                                                                                                 
 
    def __init__(self, num):                                                                                                                                                                                                                                                
        self.num = num                                                                                                                                                                                                                                                      
 
b = B(42)                                                                                                                                                                                                                                                                   
a = A()                                                                                                                                                                                                                                                                     
a.print_num()  

请您参考如下方法:

在 C/C++ 中,您可以使用前向声明获得类似的结果。在 Java 中,这并非不可能,但您可能需要反射才能使其正常工作。

这里的事情是从对象调用方法,所以变量存在。定义它的是子类还是父类是无关紧要的。一切都在运行时完成,因此在此之前不需要 num 存在。

使用你的例子:

a = A() 
a.print_num() 
 
Traceback (most recent call last): 
  File "test.py", line 18, in <module> 
    a.print_num() 
  File "test.py", line 9, in print_num 
    print b.num 
NameError: global name 'b' is not defined 

或者更奇怪的(从 C++/Java 的角度来看)版本:

class A(object): 
 
    def print_num(self): 
        print self.num 
 
 
class B(A): 
 
    def __init__(self, num): 
        self.num = num 
 
b = B(42) 
b.print_num()