`

利用metaclass实现python的aop

阅读更多

修行Python个把星期,终于有点成果了,呵呵,一个利用metaclass实现的aop。

其实python这类非常动态的语言要实现AOP是很容易的,所以首先我们要来先定义一个metaclass

然后我们要在__new__()这个metaclass 的时候动态植入方法到要调用地方法的前后。

具体代码如下:

<bgsound cep="0"><span style="color: #008080;"> 1</span><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt=""><span style="color: #000000;"><br></span><span style="color: #008080;"> 2</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt=""></span><span style="color: #800080;">__author__</span><span style="color: #000000;">=</span><span style="color: #800000;">"</span><span style="color: #800000;">alex</span><span style="color: #800000;">"</span><span style="color: #000000;"><br></span><span style="color: #008080;"> 3</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt=""></span><span style="color: #800080;">__date__</span> <span style="color: #000000;">=</span><span style="color: #800000;">"</span><span style="color: #800000;">$2008-12-5 23:54:11$</span><span style="color: #800000;">"</span><span style="color: #000000;"><br></span><span style="color: #008080;"> 4</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt=""></span><span style="color: #800080;">__name__</span><span style="color: #000000;">=</span><span style="color: #800000;">"</span><span style="color: #800000;">pyaop</span><span style="color: #800000;">"</span><span style="color: #000000;"><br></span><span style="color: #008080;"> 5</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt=""><br></span><span style="color: #008080;"> 6</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt=""></span><span style="color: #800000;">'''</span><span style="color: #800000;"><br></span><span style="color: #008080;"> 7</span><span style="color: #800000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">这个metaclass是实现AOP的基础<br></span><span style="color: #008080;"> 8</span><span style="color: #800000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt=""></span><span style="color: #800000;">'''</span><span style="color: #000000;"><br></span><span style="color: #008080;"> 9</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt=""></span><span style="color: #0000ff;">class</span><span style="color: #000000;"> pyaop(type):<br></span><span style="color: #008080;">10</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">    </span><span style="color: #800000;">'''</span><span style="color: #800000;"><br></span><span style="color: #008080;">11</span><span style="color: #800000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">    这个空方法是用来将后面的beforeop和afterop初始化成函数引用<br></span><span style="color: #008080;">12</span><span style="color: #800000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">    </span><span style="color: #800000;">'''</span><span style="color: #000000;"><br></span><span style="color: #008080;">13</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">    </span><span style="color: #0000ff;">def</span><span style="color: #000000;"> nop(self):<br></span><span style="color: #008080;">14</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">        </span><span style="color: #0000ff;">pass</span><span style="color: #000000;"><br></span><span style="color: #008080;">15</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">    </span><span style="color: #800000;">'''</span><span style="color: #800000;"><br></span><span style="color: #008080;">16</span><span style="color: #800000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">    下面这两个变量是类变量,也就是存放我们要植入的两个函数的地址的变量<br></span><span style="color: #008080;">17</span><span style="color: #800000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">    </span><span style="color: #800000;">'''</span><span style="color: #000000;"><br></span><span style="color: #008080;">18</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">    beforeop</span><span style="color: #000000;">=</span><span style="color: #000000;">nop<br></span><span style="color: #008080;">19</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">    afterop</span><span style="color: #000000;">=</span><span style="color: #000000;">nop<br></span><span style="color: #008080;">20</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">    </span><span style="color: #800000;">'''</span><span style="color: #800000;"><br></span><span style="color: #008080;">21</span><span style="color: #800000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">    设置前后两个植入函数的类函数<br></span><span style="color: #008080;">22</span><span style="color: #800000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">    </span><span style="color: #800000;">'''</span><span style="color: #000000;"><br></span><span style="color: #008080;">23</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">    @classmethod<br></span><span style="color: #008080;">24</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">    </span><span style="color: #0000ff;">def</span><span style="color: #000000;"> setbefore(self,func):<br></span><span style="color: #008080;">25</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">        pyaop.beforeop</span><span style="color: #000000;">=</span><span style="color: #000000;">func<br></span><span style="color: #008080;">26</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">    @classmethod<br></span><span style="color: #008080;">27</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">    </span><span style="color: #0000ff;">def</span><span style="color: #000000;"> setafter(self,func):<br></span><span style="color: #008080;">28</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">        pyaop.afterop</span><span style="color: #000000;">=</span><span style="color: #000000;">func<br></span><span style="color: #008080;">29</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">    </span><span style="color: #800000;">'''</span><span style="color: #800000;"><br></span><span style="color: #008080;">30</span><span style="color: #800000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">    初始化metaclass的函数,这个函数最重要的就是第四个参数,dict通过这个参数我们可以修改类的属性(方法)<br></span><span style="color: #008080;">31</span><span style="color: #800000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">    </span><span style="color: #800000;">'''</span><span style="color: #000000;"><br></span><span style="color: #008080;">32</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">    </span><span style="color: #0000ff;">def</span> <span style="color: #800080;">__new__</span><span style="color: #000000;">(mcl,name,bases,dict):<br></span><span style="color: #008080;">33</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">        </span><span style="color: #0000ff;">from</span><span style="color: #000000;"> types </span><span style="color: #0000ff;">import</span><span style="color: #000000;"> FunctionType </span><span style="color: #008000;">#</span><span style="color: #008000;">加载类型模块的FunctionType</span><span style="color: #008000;"><br></span><span style="color: #008080;">34</span><span style="color: #008000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt=""></span><span style="color: #000000;">        obj</span><span style="color: #000000;">=</span><span style="color: #000000;">object() </span><span style="color: #008000;">#</span><span style="color: #008000;">定义一个空对象的变量</span><span style="color: #008000;"><br></span><span style="color: #008080;">35</span><span style="color: #008000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt=""></span><span style="color: #000000;">        </span><span style="color: #800000;">'''</span><span style="color: #800000;"><br></span><span style="color: #008080;">36</span><span style="color: #800000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">        这个就是要植入的方法,func参数就是我们要调用的函数<br></span><span style="color: #008080;">37</span><span style="color: #800000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">        </span><span style="color: #800000;">'''</span><span style="color: #000000;"><br></span><span style="color: #008080;">38</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">        </span><span style="color: #0000ff;">def</span><span style="color: #000000;"> aop(func):<br></span><span style="color: #008080;">39</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">            </span><span style="color: #800000;">'''</span><span style="color: #800000;"><br></span><span style="color: #008080;">40</span><span style="color: #800000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">            我们用这个函数来代替将要调用的函数<br></span><span style="color: #008080;">41</span><span style="color: #800000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">            </span><span style="color: #800000;">'''</span><span style="color: #000000;"><br></span><span style="color: #008080;">42</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">            </span><span style="color: #0000ff;">def</span><span style="color: #000000;"> wrapper(</span><span style="color: #000000;">*</span><span style="color: #000000;">args, </span><span style="color: #000000;">**</span><span style="color: #000000;">kwds):<br></span><span style="color: #008080;">43</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">                pyaop.beforeop(obj) </span><span style="color: #008000;">#</span><span style="color: #008000;">调用前置函数</span><span style="color: #008000;"><br></span><span style="color: #008080;">44</span><span style="color: #008000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt=""></span><span style="color: #000000;">                value </span><span style="color: #000000;">=</span><span style="color: #000000;"> func(</span><span style="color: #000000;">*</span><span style="color: #000000;">args, </span><span style="color: #000000;">**</span><span style="color: #000000;">kwds) </span><span style="color: #008000;">#</span><span style="color: #008000;">调用本来要调用的函数</span><span style="color: #008000;"><br></span><span style="color: #008080;">45</span><span style="color: #008000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt=""></span><span style="color: #000000;">                pyaop.afterop(obj) </span><span style="color: #008000;">#</span><span style="color: #008000;">调用后置函数</span><span style="color: #008000;"><br></span><span style="color: #008080;">46</span><span style="color: #008000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt=""></span><span style="color: #000000;">                </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> value </span><span style="color: #008000;">#</span><span style="color: #008000;">返回</span><span style="color: #008000;"><br></span><span style="color: #008080;">47</span><span style="color: #008000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt=""></span><span style="color: #000000;">            </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> wrapper<br></span><span style="color: #008080;">48</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">        </span><span style="color: #008000;">#</span><span style="color: #008000;">在类的成员列表中查找即将调用的函数</span><span style="color: #008000;"><br></span><span style="color: #008080;">49</span><span style="color: #008000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt=""></span><span style="color: #000000;">        </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> attr, value </span><span style="color: #0000ff;">in</span><span style="color: #000000;"> dict.iteritems():<br></span><span style="color: #008080;">50</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">            </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> isinstance(value, FunctionType):<br></span><span style="color: #008080;">51</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">                dict[attr] </span><span style="color: #000000;">=</span><span style="color: #000000;"> aop(value) </span><span style="color: #008000;">#</span><span style="color: #008000;">找到后用aop这个函数替换之</span><span style="color: #008000;"><br></span><span style="color: #008080;">52</span><span style="color: #008000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt=""></span><span style="color: #000000;">        obj</span><span style="color: #000000;">=</span><span style="color: #000000;">super(pyaop, mcl).</span><span style="color: #800080;">__new__</span><span style="color: #000000;">(mcl, name, bases, dict) </span><span style="color: #008000;">#</span><span style="color: #008000;">调用父类的__new__()创建self</span><span style="color: #008000;"><br></span><span style="color: #008080;">53</span><span style="color: #008000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt=""></span><span style="color: #000000;">        </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> obj<br></span><span style="color: #008080;">54</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">    </span></bgsound>

 

使用的时候,如果我们要拦截一个类A的方法调用,就这样子:

 

<bgsound cep="1"><span style="color: #008080;"> 1</span><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt=""><span style="color: #0000ff;">class</span><span style="color: #000000;"> A(object):<br></span><span style="color: #008080;"> 2</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">    </span><span style="color: #800080;">__metaclass__</span> <span style="color: #000000;">=</span><span style="color: #000000;"> pyaop<br></span><span style="color: #008080;"> 3</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">    </span><span style="color: #0000ff;">def</span><span style="color: #000000;"> foo(self):<br></span><span style="color: #008080;"> 4</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">        total </span><span style="color: #000000;">=</span><span style="color: #000000;"> 0<br></span><span style="color: #008080;"> 5</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">        </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> i </span><span style="color: #0000ff;">in</span><span style="color: #000000;"> range(</span><span style="color: #000000;">100000</span><span style="color: #000000;">):<br></span><span style="color: #008080;"> 6</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">            total </span><span style="color: #000000;">=</span><span style="color: #000000;"> total</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;"><br></span><span style="color: #008080;"> 7</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">        </span><span style="color: #0000ff;">print</span><span style="color: #000000;"> total<br></span><span style="color: #008080;"> 8</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt=""><br></span><span style="color: #008080;"> 9</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">    </span><span style="color: #0000ff;">def</span><span style="color: #000000;"> foo2(self):<br></span><span style="color: #008080;">10</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">        </span><span style="color: #0000ff;">from</span><span style="color: #000000;"> time </span><span style="color: #0000ff;">import</span><span style="color: #000000;"> sleep<br></span><span style="color: #008080;">11</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">        total </span><span style="color: #000000;">=</span><span style="color: #000000;"> 0<br></span><span style="color: #008080;">12</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">        </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> i </span><span style="color: #0000ff;">in</span><span style="color: #000000;"> range(</span><span style="color: #000000;">100000</span><span style="color: #000000;">):<br></span><span style="color: #008080;">13</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">            total </span><span style="color: #000000;">=</span><span style="color: #000000;"> total</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;"><br></span><span style="color: #008080;">14</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">            sleep(</span><span style="color: #000000;">0.0001</span><span style="color: #000000;">)<br></span><span style="color: #008080;">15</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">        </span><span style="color: #0000ff;">print</span><span style="color: #000000;"> total</span></bgsound>

 

最后我们只需要:

 

<bgsound cep="2"><span style="color: #008080;"> 1</span><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt=""><span style="color: #0000ff;">def</span><span style="color: #000000;"> beforep(self):<br></span><span style="color: #008080;"> 2</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">    </span><span style="color: #0000ff;">print</span><span style="color: #000000;">(</span><span style="color: #800000;">'</span><span style="color: #800000;">before</span><span style="color: #800000;">'</span><span style="color: #000000;">)<br></span><span style="color: #008080;"> 3</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt=""></span><span style="color: #0000ff;">def</span><span style="color: #000000;"> afterp(self):<br></span><span style="color: #008080;"> 4</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">    </span><span style="color: #0000ff;">print</span><span style="color: #000000;">(</span><span style="color: #800000;">'</span><span style="color: #800000;">after</span><span style="color: #800000;">'</span><span style="color: #000000;">)<br></span><span style="color: #008080;"> 5</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt=""><br></span><span style="color: #008080;"> 6</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt=""></span><span style="color: #0000ff;">if</span> <span style="color: #800080;">__name__</span> <span style="color: #000000;">==</span> <span style="color: #800000;">"</span><span style="color: #800000;">__main__</span><span style="color: #800000;">"</span><span style="color: #000000;">:<br></span><span style="color: #008080;"> 7</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">    pyaop.setbefore(beforep)<br></span><span style="color: #008080;"> 8</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">    pyaop.setafter(afterp)<br></span><span style="color: #008080;"> 9</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">    a</span><span style="color: #000000;">=</span><span style="color: #000000;">A()<br></span><span style="color: #008080;">10</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">    a.foo()<br></span><span style="color: #008080;">11</span><span style="color: #000000;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="">    a.foo2()</span></bgsound>

 这样子在执行代码的时候就得到了如下结果

before
100000
after
before
100000
after

这段代码耗时一天才调通(汗颜),是基于园里另外一贴 http://www.cnblogs.com/cavingdeep/archive/2006/08/22/483056.html 

并感谢python讨论群69828975里的大虾Seewind(310380)给予的无私帮助

欢迎各位python大牛给予斧正

分享到:
评论

相关推荐

    基于Python语言环境的轻量AOP工具的设计实现.zip

    我们可以通过元类(Metaclass)或者在导入模块时动态修改对象来实现这一点。 5. 使用示例:例如,我们可以定义一个日志切面,包含开始和结束的日志记录装饰器。当应用到某个函数上时,这些装饰器会在函数执行前后...

    详解python metaclass(元类)

    Python中的元类(metaclass)是一个高级特性,用于控制类的创建过程。元类是创建普通类的类,它允许我们对类的行为进行定制。在Python中,一切皆为对象,包括类,而类是由元类创建的。默认情况下,Python的`type`...

    超全面python面试题

    2 Python中的元类(metaclass) 3 @staticmethod和@classmethod 4 类变量和实例变量 5 Python自省 6 字典推导式 7 Python中单下划线和双下划线 8 字符串格式化:\x和.format 9 迭代器和生成器 10 *args and **kwargs 11...

    Python高级编程.zip

    Python支持元编程,通过`type()`函数、类装饰器、元类(metaclass)等工具,你可以动态创建类型和对象,实现自定义的类行为。 接着是面向切面编程(Aspect-Oriented Programming, AOP)。虽然Python社区对此并不...

    Python Interview

    Python中的元类 (Metaclass) 元类是Python中的高级特性之一,它允许我们控制或定制类的行为。简单来说,元类就是创建类的“类”。在Python中,所有类都是`type`类的实例,这意味着我们可以使用`type`来创建新的类...

    python转java方法.docx

    Python 中的元类(metaclass)在 Java 中没有直接对应的概念,可能需要使用工厂方法或其他设计模式来模拟。 4. **函数与方法**:Python 中的函数定义可以有默认参数和可变参数,Java 中则需要分别处理。Python 的...

    深入理解python元类

    class MetaClass(type): def __new__(cls, name, bases, namespace): # 在这里可以对类的定义进行修改 return super().__new__(cls, name, bases, namespace) ``` 3. **自注册**:元类可以用于自动将新创建的...

    Python面试内容很多,干货满满

    Python中的元类(metaclass) 元类是Python中一个高级特性,它允许开发者控制类的创建过程。元类本质上就是用来创建类的类。通常情况下,我们定义一个类时,默认使用`type`作为元类。通过定义自己的元类,可以在创建...

    精通python设计模式源码

    例如,通过元类(metaclass)可以轻松实现单例模式,装饰器可以用于实现装饰器模式,而面向切面编程(AOP)的概念则可以用于实现观察者模式等。 在"mpdp-code-master"这个压缩包中,你可能会找到每个设计模式的实例...

    python面试题

    Python中的元类(metaclass) 元类是用于创建类的类,它允许开发者控制类的行为。在Python中,所有类都是由`type`元类创建的,默认情况下,Python类的元类就是`type`。通过定义自己的元类,可以定制类的创建过程,...

    Python中type的构造函数参数含义说明

    在Python编程语言中,`type` 是一个内置的类型,它不仅用于判断对象的类型,还可以作为元类(metaclass)来创建新的类型。元类是控制类行为的一种方式,它可以自定义类的创建过程。在本篇内容中,我们将深入探讨`...

    Boo Language Guide

    你可以通过元类(metaclass)和属性(property)来定制类的行为,甚至在运行时动态创建和修改类型。这对于构建框架和库非常有用,因为它允许代码对自身的结构进行操作。 3. 面向切面编程(AOP)支持:Boo内置了对...

    groovy-docs-1.5.4.zip

    `MetaClass`接口和`ExpandoMetaClass`是实现这一功能的关键。 通过阅读`groovy-docs-1.5.4.zip`中的文档,你可以深入了解这些概念,以及更多关于Groovy语言的细节,包括错误处理、异常、集合操作、字符串处理、网络...

    groovy_Handson

    通过MetaClass,我们可以添加方法或属性到任何对象,甚至包括Java内置类。这对于实现AOP(面向切面编程)或创建灵活的框架非常有用。 此外,Groovy还提供了许多便利的集合操作,如each、collect、find等,这些是...

Global site tag (gtag.js) - Google Analytics