最新消息:

用Descriptor来实现类级属性(Property)

python admin 3347浏览 0评论

上篇文章简单介绍了python中描述器(Descriptor)的概念和使用,有心的同学估计已经Get√了该技能。本篇文章通过一个Descriptor的使用场景再次给出一个案例,让不了解情况的同学可以更容易理解。

先说说decorator

这两个单词确实是有些相似,同时在使用中也是形影不离。这也给人造成了理解上的困难,说装饰器和描述器到底是怎么回事,为什么非得用一个@符号再加上描述器才行。

很多文章也都把这俩结合着讲,我自己看完之后都会觉得很绕。其实学习一个知识点,和做项目开发一个功能是一样的。在功能拆分的时候我们都会尽量的把任务拆分的足够小,然后才分配到开发者头上。这样保证各个任务的独立性,完整性,并且易于做进度管理。在任务开发的时候也不能把你的任务都放到一个函数/接口中去做,以避免各功能间产生高耦合的状况,导致后期难以维护。

再说回到学习一个技术点,如果你总是尝试一下子就要掌握两个或多个技术点,结果可能是忙活了半天,发现还是晕头转向。

擦,好像扯远了。

说Descriptor是Descriptor, Decorator是Decorator,遇到不懂的地方,各个击破,哪里不懂点哪里。所以先说Decorator, 关键点是你要意识到这就是一个语法糖 。所谓语法糖就是让你可以用简单的方式写代码。本质上装饰器(Decorator)就是这样:

<span class="keyword" style="font-weight: bold; color: #3b0d06;">def</span> <span class="name function">decorator</span><span class="punctuation">(</span><span class="name">func</span><span class="punctuation">):</span>
    <span class="keyword" style="font-weight: bold; color: #3b0d06;">def</span> <span class="name function">wrapper</span><span class="punctuation">():</span>
        <span class="keyword" style="font-weight: bold; color: #3b0d06;">print</span> <span class="literal string" style="color: #0c5404;">'in decorator'</span>
        <span class="name">func</span><span class="punctuation">()</span>
    <span class="keyword" style="font-weight: bold; color: #3b0d06;">return</span> <span class="name">wrapper</span>

<span class="keyword" style="font-weight: bold; color: #3b0d06;">def</span> <span class="name function">func</span><span class="punctuation">():</span>
    <span class="keyword" style="font-weight: bold; color: #3b0d06;">print</span> <span class="literal string" style="color: #0c5404;">'in func'</span>

<span class="comment" style="color: #5c6576;"># 把func装饰一下</span>
<span class="name">func</span> <span class="operator">=</span> <span class="name">decorator</span><span class="punctuation">(</span><span class="name">func</span><span class="punctuation">)</span>  <span class="comment" style="color: #5c6576;"># 左边的func其实是那个wrapper, 你执行它的时候会,它会帮你执行func()</span>
<span class="comment" style="color: #5c6576;"># 等同于你在定义func的时候加上@</span>
<span class="name decorator">@decorator</span>
<span class="keyword" style="font-weight: bold; color: #3b0d06;">def</span> <span class="name function">func</span><span class="punctuation">():</span>
    <span class="keyword" style="font-weight: bold; color: #3b0d06;">print</span> <span class="literal string" style="color: #0c5404;">'in func'</span>

正题:通过Descriptor来做一个类级的Property

常见的Property是这样的:

<span class="keyword" style="font-weight: bold; color: #3b0d06;">class</span> <span class="name class">Foo</span><span class="punctuation">(</span><span class="name builtin" style="color: #352b84;">object</span><span class="punctuation">):</span>
    <span class="name">_name</span> <span class="operator">=</span> <span class="literal string" style="color: #0c5404;">'the5fire'</span>

    <span class="name decorator">@property</span>
    <span class="keyword" style="font-weight: bold; color: #3b0d06;">def</span> <span class="name function">name</span><span class="punctuation">(</span><span class="name builtin pseudo" style="color: #352b84;">self</span><span class="punctuation">):</span>
        <span class="keyword" style="font-weight: bold; color: #3b0d06;">return</span> <span class="name builtin pseudo" style="color: #352b84;">self</span><span class="operator">.</span><span class="name">_name</span>

这中property的使用,是实例级的应用。因为只有在 foo = Foo() 之后,才可以 foo.name 。

但是如果我需要一个类级的属性应该怎么做呢,就像是 classmethod一样,不需要实例化类我就可以调用。对应的需求是这样的,定义了一个基类DBManage:

<span class="keyword" style="font-weight: bold; color: #3b0d06;">class</span> <span class="name class">DBManage</span><span class="punctuation">(</span><span class="name builtin" style="color: #352b84;">object</span><span class="punctuation">):</span>
    <span class="name decorator">@classmethod</span>
    <span class="keyword" style="font-weight: bold; color: #3b0d06;">def</span> <span class="name function">table_name</span><span class="punctuation">(</span><span class="name">cls</span><span class="punctuation">):</span>
        <span class="keyword" style="font-weight: bold; color: #3b0d06;">return</span> <span class="name">cls</span><span class="operator">.</span><span class="name">__name__</span><span class="operator">.</span><span class="name">lower</span><span class="punctuation">()</span>

    <span class="name decorator">@classmethod</span>
    <span class="keyword" style="font-weight: bold; color: #3b0d06;">def</span> <span class="name function">select_all</span><span class="punctuation">(</span><span class="name">cls</span><span class="punctuation">):</span>
        <span class="name">sql</span> <span class="operator">=</span> <span class="literal string" style="color: #0c5404;">"SELECT * FROM </span><span class="literal string interpol" style="color: #0c5404;">%s</span><span class="literal string" style="color: #0c5404;">"""</span> <span class="operator">%</span> <span class="name">cls</span><span class="operator">.</span><span class="name">table_name</span><span class="punctuation">()</span>
        <span class="comment" style="color: #5c6576;"># 执行这个语句的代码</span>
        <span class="keyword" style="font-weight: bold; color: #3b0d06;">return</span> <span class="name">result</span>

这其实一个对应着数据库中某张表的基础模型,我希望其他的Model都来继承它,然后可以重用这个table_name的方法(目前还是方法)。

我只需要这么定义User模型即可:

<span class="keyword" style="font-weight: bold; color: #3b0d06;">class</span> <span class="name class">User</span><span class="punctuation">(</span><span class="name">DBManage</span><span class="punctuation">):</span>
    <span class="keyword" style="font-weight: bold; color: #3b0d06;">pass</span>

然后这么定义Post模型:

<span class="keyword" style="font-weight: bold; color: #3b0d06;">class</span> <span class="name class">Post</span><span class="punctuation">(</span><span class="name">DBManage</span><span class="punctuation">):</span>
    <span class="keyword" style="font-weight: bold; color: #3b0d06;">pass</span>

这样我如果需要查所有的User数据,只需要 User.select_all() 即可,同理Post也是如此Post.select_all() 。但此时发现一个有点不爽的事情。那就是基类中的 cls.table_name() 这个代码,table_name看起来就是属性,却需要用调用方法的方式获取。不妥。

于是自定义了一个classproperty:

<span class="keyword" style="font-weight: bold; color: #3b0d06;">class</span> <span class="name class">classproperty</span><span class="punctuation">(</span><span class="name builtin" style="color: #352b84;">object</span><span class="punctuation">):</span>
    <span class="keyword" style="font-weight: bold; color: #3b0d06;">def</span> <span class="name function">__init__</span><span class="punctuation">(</span><span class="name builtin pseudo" style="color: #352b84;">self</span><span class="punctuation">,</span> <span class="name">func</span><span class="punctuation">):</span>
        <span class="name builtin pseudo" style="color: #352b84;">self</span><span class="operator">.</span><span class="name">func</span> <span class="operator">=</span> <span class="name">func</span>

    <span class="keyword" style="font-weight: bold; color: #3b0d06;">def</span> <span class="name function">__get__</span><span class="punctuation">(</span><span class="name builtin pseudo" style="color: #352b84;">self</span><span class="punctuation">,</span> <span class="name">instance</span><span class="punctuation">,</span> <span class="name">klass</span><span class="punctuation">):</span>
        <span class="keyword" style="font-weight: bold; color: #3b0d06;">return</span> <span class="name builtin pseudo" style="color: #352b84;">self</span><span class="operator">.</span><span class="name">func</span><span class="punctuation">(</span><span class="name">klass</span><span class="punctuation">)</span>

这需要这样,我在DBManage中的代码就可以改为:

<span class="keyword" style="font-weight: bold; color: #3b0d06;">class</span> <span class="name class">DBManage</span><span class="punctuation">(</span><span class="name builtin" style="color: #352b84;">object</span><span class="punctuation">):</span>
    <span class="name decorator">@classproperty</span>
    <span class="keyword" style="font-weight: bold; color: #3b0d06;">def</span> <span class="name function">table_name</span><span class="punctuation">(</span><span class="name">cls</span><span class="punctuation">):</span>
        <span class="keyword" style="font-weight: bold; color: #3b0d06;">return</span> <span class="name">cls</span><span class="operator">.</span><span class="name">__name__</span><span class="operator">.</span><span class="name">lower</span><span class="punctuation">()</span>

    <span class="name decorator">@classmethod</span>
    <span class="keyword" style="font-weight: bold; color: #3b0d06;">def</span> <span class="name function">select_all</span><span class="punctuation">(</span><span class="name">cls</span><span class="punctuation">):</span>
        <span class="name">sql</span> <span class="operator">=</span> <span class="literal string" style="color: #0c5404;">"SELECT * FROM </span><span class="literal string interpol" style="color: #0c5404;">%s</span><span class="literal string" style="color: #0c5404;">"""</span> <span class="operator">%</span> <span class="name">cls</span><span class="operator">.</span><span class="name">table_name</span>  <span class="comment" style="color: #5c6576;"># 多么直观</span>

这就是Descriptor另外的一个使用案例了。

可能有人或有一个小疑问:为毛你不是在sql赋值时直接 sql = "SELECT * FROM %s" % cls.__name__.lower() 。这个问题,问的非常好,原因就一个字:懒。懒得以后每次都得敲那么多代码。

 

转载请注明:爱开源 » 用Descriptor来实现类级属性(Property)

您必须 登录 才能发表评论!