- 浏览: 145713 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
老八牛:
利用迭代器让异步操作更加“人性化”-山寨版的AsyncEnumerator -
老八牛:
为什么看不到代码?
利用迭代器让异步操作更加“人性化”-山寨版的AsyncEnumerator -
xi4nyu:
如果启动Application中的settings 的debu ...
玩蛇记-使用Tornado构建高性能Web之二-autoreload -
huacnlee:
"且在python下工作多日才发现原来在.NET下的 ...
玩蛇记-使用tornado构建高性能Web应用之一 -
jasongreen:
异步数据库操作,在web上有什么作用吗?
玩蛇记-使用tornado构建高性能Web应用之一
修行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大牛给予斧正
发表评论
-
关于ORM和内存数据库的遐想
2007-01-23 13:21 570最近有消息说韩国电信 ... -
继续ORM-欧德巴赫猜想-Mapping
2007-01-23 14:34 666最近从项目组单离出来开始在公司实施过程化管理,整个QA Off ... -
剑走偏锋,小心走火入魔
2007-01-23 15:07 706这是很久前写好的文字,闲得无聊就发上来,几个月前的感想,上午一 ... -
手把手教你写ORM(三)
2007-01-24 11:50 605昨天处于晕死状态,少写了一个组件,还需要一个组件用来专门管理C ... -
手把手教你写ORM(四)
2007-01-24 13:51 641现在中午不睡一会儿就头晕。前一篇有人留言说为什么不写web.c ... -
手把手教你写ORM(五)
2007-01-24 15:29 604CMMI是魔鬼继续上面的内容,这里我们要实现一个插件的结构来动 ... -
谈谈我们的学习和我们的Blog
2007-01-24 20:07 407第一,学习编程是一个很枯燥的过程,所以我们更要讲究效率(要把有 ... -
手把手教你写代码生成器(也算ORM的续)
2007-01-25 11:45 724因为ORM还是需要配置,还是需要EntityObject,所以 ... -
粒度细到控件的权限管理系统的设计(概要篇)
2007-01-25 21:40 1066其实这个设计是已经做过了,那个时候我才进公司还在试用期,给我的 ... -
粒度细到控件的权限管理组件(构想篇)
2007-01-26 10:34 747说老实话我现在还没开 ... -
手把手教你写ORM大全篇
2007-01-26 19:36 680根据dudu boss的建议将本系列作一个归纳,下一个系列正在 ... -
架构设计的非侵入性原则
2007-01-27 00:41 698最近常常看到JAVA社区热 ... -
手把手教你可复用SSO组件的设计(原理篇)
2007-01-27 14:55 758在结构设计上复用性 ... -
对《万事欠备设计先行》的一点想法,兼谈XP和CMMI
2007-01-29 09:31 650周末陪女友,故沉默了,其实大脑并没有沉默,之前看到《万事欠备设 ... -
手把手教你可复用的SSO组件设计(设计篇)
2007-01-29 16:24 592周末陪女朋友去了,没写,告罪,上班后急忙补上。 这里说到了可复 ... -
手把手教你可复用的SSO组件设计(实现篇)
2007-01-29 22:30 610费了一夜的功夫写完这些代码,有些凌乱,望见谅。 首先是对加密解 ... -
玩具级嵌入式内存对象数据库^V^
2007-02-01 19:46 495纯粹是为了好玩:} 最近几天很忙所以写得少了,昨天在清理硬盘的 ... -
差之毫厘谬以千里-计算中的精度问题
2007-02-27 10:04 586如果你只是i++来作计数 ... -
动态语言,涅磐重生还是死路一条?
2007-03-06 10:31 580最近花时间一直在看python和ruby,为了在Web应用又看 ... -
ASP.NET's MVC is what a joke!
2007-03-08 13:43 477很早前还在毁人不倦的 ...
相关推荐
我们可以通过元类(Metaclass)或者在导入模块时动态修改对象来实现这一点。 5. 使用示例:例如,我们可以定义一个日志切面,包含开始和结束的日志记录装饰器。当应用到某个函数上时,这些装饰器会在函数执行前后...
Python中的元类(metaclass)是一个高级特性,用于控制类的创建过程。元类是创建普通类的类,它允许我们对类的行为进行定制。在Python中,一切皆为对象,包括类,而类是由元类创建的。默认情况下,Python的`type`...
2 Python中的元类(metaclass) 3 @staticmethod和@classmethod 4 类变量和实例变量 5 Python自省 6 字典推导式 7 Python中单下划线和双下划线 8 字符串格式化:\x和.format 9 迭代器和生成器 10 *args and **kwargs 11...
Python支持元编程,通过`type()`函数、类装饰器、元类(metaclass)等工具,你可以动态创建类型和对象,实现自定义的类行为。 接着是面向切面编程(Aspect-Oriented Programming, AOP)。虽然Python社区对此并不...
Python中的元类 (Metaclass) 元类是Python中的高级特性之一,它允许我们控制或定制类的行为。简单来说,元类就是创建类的“类”。在Python中,所有类都是`type`类的实例,这意味着我们可以使用`type`来创建新的类...
Python 中的元类(metaclass)在 Java 中没有直接对应的概念,可能需要使用工厂方法或其他设计模式来模拟。 4. **函数与方法**:Python 中的函数定义可以有默认参数和可变参数,Java 中则需要分别处理。Python 的...
class MetaClass(type): def __new__(cls, name, bases, namespace): # 在这里可以对类的定义进行修改 return super().__new__(cls, name, bases, namespace) ``` 3. **自注册**:元类可以用于自动将新创建的...
Python中的元类(metaclass) 元类是Python中一个高级特性,它允许开发者控制类的创建过程。元类本质上就是用来创建类的类。通常情况下,我们定义一个类时,默认使用`type`作为元类。通过定义自己的元类,可以在创建...
例如,通过元类(metaclass)可以轻松实现单例模式,装饰器可以用于实现装饰器模式,而面向切面编程(AOP)的概念则可以用于实现观察者模式等。 在"mpdp-code-master"这个压缩包中,你可能会找到每个设计模式的实例...
Python中的元类(metaclass) 元类是用于创建类的类,它允许开发者控制类的行为。在Python中,所有类都是由`type`元类创建的,默认情况下,Python类的元类就是`type`。通过定义自己的元类,可以定制类的创建过程,...
在Python编程语言中,`type` 是一个内置的类型,它不仅用于判断对象的类型,还可以作为元类(metaclass)来创建新的类型。元类是控制类行为的一种方式,它可以自定义类的创建过程。在本篇内容中,我们将深入探讨`...
你可以通过元类(metaclass)和属性(property)来定制类的行为,甚至在运行时动态创建和修改类型。这对于构建框架和库非常有用,因为它允许代码对自身的结构进行操作。 3. 面向切面编程(AOP)支持:Boo内置了对...
`MetaClass`接口和`ExpandoMetaClass`是实现这一功能的关键。 通过阅读`groovy-docs-1.5.4.zip`中的文档,你可以深入了解这些概念,以及更多关于Groovy语言的细节,包括错误处理、异常、集合操作、字符串处理、网络...
通过MetaClass,我们可以添加方法或属性到任何对象,甚至包括Java内置类。这对于实现AOP(面向切面编程)或创建灵活的框架非常有用。 此外,Groovy还提供了许多便利的集合操作,如each、collect、find等,这些是...