<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
    <channel>
        <title>Python - Category - TommyLike Blog</title>
        <link>https://tommylike.com/categories/python/</link>
        <description>Python - Category - TommyLike Blog</description>
        <generator>Hugo -- gohugo.io</generator><language>en</language><managingEditor>tommylikehu@gmail.com (TommyLike)</managingEditor>
            <webMaster>tommylikehu@gmail.com (TommyLike)</webMaster><copyright>This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.</copyright><lastBuildDate>Sun, 14 Aug 2016 21:59:44 &#43;0800</lastBuildDate><atom:link href="https://tommylike.com/categories/python/" rel="self" type="application/rss+xml" /><item>
    <title>Python常见特性入门(一)</title>
    <link>https://tommylike.com/python-advance/</link>
    <pubDate>Sun, 14 Aug 2016 21:59:44 &#43;0800</pubDate>
    <author>TommyLike</author>
    <guid>https://tommylike.com/python-advance/</guid>
    <description><![CDATA[Python常见特性入门 装饰器 有过AOP编程经历的人对以下的场景肯定都不陌生:我们暴露了API接口给客户端调用，基于运维的考虑，我们需要自动拦截API接口的耗时参数等信息，通常的方式就是在基类中包装一层或者使用拦截器，大致的代码如下:
1 2 3 4 5 6 7 public string WrapExecute(string fake_param) { ExecuteBefore(fake_param); var result = Execute(); ExecuteAfter(result); return result } Python的装饰器天生就可以承担类似的工作,下面是一个简单的装饰器:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 def execute_before(): print(&#39;this is before&#39;) def execute_after(): print(&#39;this is after&#39;) def decorator(func): def wrapper(): execute_before() func() execute_after() return wrapper def execute(): print(&#39;this is executing&#39;) execute = decorator(execute) 跟我们在其他语言中的用法保持一致，当然我们的python在此之上，还提供了更便捷的方式:@,python中以@开头并紧跟装饰器的名字可理解成给对应的对应的对象使用装饰器，所以，上面的调用方式，可以优化成:]]></description>
</item>
</channel>
</rss>
