Home >Backend Development >Python Tutorial >Python 比 Java 牛在哪?
<code class="language-python"><span class="k">class</span> <span class="nc">A</span><span class="p">:</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">print</span> <span class="s">"init A"</span>
<span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">print</span> <span class="s">"run A"</span>
<span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">A</span><span class="p">)</span><span class="o">.</span><span class="n">start</span><span class="p">()</span> <span class="c">#: 在另一个线程执行A()</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">A</span><span class="p">()</span>
<span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">a</span><span class="o">.</span><span class="n">run</span><span class="p">)</span><span class="o">.</span><span class="n">start</span><span class="p">()</span> <span class="c">#: 在另一个线程执行a.run() ... </span>
<span class="c"># 惊呆没有,这是怎么做到的?? 也许你该看看描述符的概念</span>
</code>
因为python很灵活,一定程度上函数也可以传参和注入,所以代码的灵活性要大的多,而Java在Java 8之前是无法做到函数传参的,所以这种情况下,Java开发者只能写大量的匿名类去注入代码块(这点相比,还是ruby块代码注入更牛逼,不是吗)。