<p>Whenever we create a block, we need to set its parent block to the current block, hence the Python class <codeclass="docutils literal"><spanclass="pre">Program</span></code> needs to maintain a data member <codeclass="docutils literal"><spanclass="pre">current_block</span></code>.</p>
<spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">proto</span><spanclass="o">=</span><spanclass="n">core</span><spanclass="o">.</span><spanclass="n">NewProgram</span><spanclass="p">()</span><spanclass="c1"># a C++ ProgramDesc pointer.</span>
<spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">desc</span><spanclass="o">=</span><spanclass="n">core</span><spanclass="o">.</span><spanclass="n">NewProgram</span><spanclass="p">()</span><spanclass="c1"># a C++ ProgramDesc pointer.</span>
<spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">blocks</span><spanclass="o">.</span><spanclass="n">append</span><spanclass="p">(</span><spanclass="n">Block</span><spanclass="p">(</span><spanclass="bp">self</span><spanclass="p">,</span><spanclass="o">-</span><spanclass="mi">1</span><spanclass="p">))</span><spanclass="c1"># the global block</span>
<spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">current_block</span><spanclass="o">=</span><spanclass="mi">0</span><spanclass="c1"># initialized to the global block</span>
<p><codeclass="docutils literal"><spanclass="pre">Operator</span></code> creates the <codeclass="docutils literal"><spanclass="pre">OpDesc</span></code> message in C++ space, so that it can call the <codeclass="docutils literal"><spanclass="pre">InferShape</span></code> function, which is in C++.</p>
<p>Whenever we create a block, we need to set its parent block to the current block, hence the Python class <codeclass="docutils literal"><spanclass="pre">Program</span></code> needs to maintain a data member <codeclass="docutils literal"><spanclass="pre">current_block</span></code>.</p>
<spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">proto</span><spanclass="o">=</span><spanclass="n">core</span><spanclass="o">.</span><spanclass="n">NewProgram</span><spanclass="p">()</span><spanclass="c1"># a C++ ProgramDesc pointer.</span>
<spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">desc</span><spanclass="o">=</span><spanclass="n">core</span><spanclass="o">.</span><spanclass="n">NewProgram</span><spanclass="p">()</span><spanclass="c1"># a C++ ProgramDesc pointer.</span>
<spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">blocks</span><spanclass="o">.</span><spanclass="n">append</span><spanclass="p">(</span><spanclass="n">Block</span><spanclass="p">(</span><spanclass="bp">self</span><spanclass="p">,</span><spanclass="o">-</span><spanclass="mi">1</span><spanclass="p">))</span><spanclass="c1"># the global block</span>
<spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">current_block</span><spanclass="o">=</span><spanclass="mi">0</span><spanclass="c1"># initialized to the global block</span>
<p><codeclass="docutils literal"><spanclass="pre">Operator</span></code> creates the <codeclass="docutils literal"><spanclass="pre">OpDesc</span></code> message in C++ space, so that it can call the <codeclass="docutils literal"><spanclass="pre">InferShape</span></code> function, which is in C++.</p>