深入挖掘 property

Author Avatar
doubledna 2017 年 02 月 04 日

<h3></h3>
在 Python 中,property() 是一个内置函数,用于创建和返回一个 property 对象。该函数的签名为:
<div id="crayon-58953244abcbf566261295" class="crayon-syntax crayon-theme-github crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-always">
<div class="crayon-toolbar" data-settings=" show">
<div class="crayon-tools">
<div class="crayon-button crayon-nums-button crayon-pressed" title=" 切换是否显示行编号 "></div>
<div class="crayon-button crayon-plain-button" title=" 纯文本显示代码 "></div>
<div class="crayon-button crayon-wrap-button" title=" 切换自动换行 "></div>
<div class="crayon-button crayon-expand-button" title=" 点击展开代码 "></div>
<div class="crayon-button crayon-copy-button" title=" 复制代码 "></div>
<div class="crayon-button crayon-popup-button" title=" 在新窗口中显示代码 "></div>
<span class="crayon-language">Python</span></div>
</div>
<div class="crayon-plain-wrap"></div>
<div class="crayon-main">

<div class="crayon-nums-content"> <div class="crayon-num" data-line="crayon-58953244abcbf566261295-1">1</div> </div> <div class="crayon-pre"> <div id="crayon-58953244abcbf566261295-1" class="crayon-line"><span class="crayon-k ">property</span><span class="crayon-sy">(</span><span class="crayon-v">fget</span><span class="crayon-o">=</span><span class="crayon-t">None</span><span class="crayon-sy">,</span> <span class="crayon-v">fset</span><span class="crayon-o">=</span><span class="crayon-t">None</span><span class="crayon-sy">,</span> <span class="crayon-v">fdel</span><span class="crayon-o">=</span><span class="crayon-t">None</span><span class="crayon-sy">,</span> <span class="crayon-v">doc</span><span class="crayon-o">=</span><span class="crayon-t">None</span><span class="crayon-sy">)</span></div> </div>

</div>
</div>
这里,fget 是一个获取属性值的函数,fset 是一个设置属性值的函数,fdel 是一个删除属性的函数,doc 是一个字符串(类似于注释)。从函数实现上看,这些函数参数都是可选的。所以,可以按照如下的方式简单的创建一个 property 对象。
<div id="crayon-58953244abcc2903842822" class="crayon-syntax crayon-theme-github crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-always">
<div class="crayon-toolbar" data-settings=" show">
<div class="crayon-tools">
<div class="crayon-button crayon-nums-button crayon-pressed" title=" 切换是否显示行编号 "></div>
<div class="crayon-button crayon-plain-button" title=" 纯文本显示代码 "></div>
<div class="crayon-button crayon-wrap-button" title=" 切换自动换行 "></div>
<div class="crayon-button crayon-copy-button" title=" 复制代码 "></div>
<div class="crayon-button crayon-popup-button" title=" 在新窗口中显示代码 "></div>
<span class="crayon-language">Python</span></div>
</div>
<div class="crayon-plain-wrap"></div>
<div class="crayon-main">

<div class="crayon-nums-content"> <div class="crayon-num" data-line="crayon-58953244abcc2903842822-1">1</div> <div class="crayon-num crayon-striped-num" data-line="crayon-58953244abcc2903842822-2">2</div> </div> <div class="crayon-pre"> <div id="crayon-58953244abcc2903842822-1" class="crayon-line"><span class="crayon-o">>>></span> <span class="crayon-k ">property</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div> <div id="crayon-58953244abcc2903842822-2" class="crayon-line crayon-striped-line"><span class="crayon-o"><</span><span class="crayon-k ">property</span> <span class="crayon-k ">object</span> <span class="crayon-i">at</span> <span class="crayon-cn">0x0000000003239B38</span><span class="crayon-o">></span></div> </div>

</div>
</div>
Property 对象有三个方法,getter(), setter() 和 delete(),用来在对象创建后设置 fget,fset 和 fdel。这就意味着,这行代码:temperature = property(get_temperature,set_temperature) 可以被分解为:
<div id="crayon-58953244abcc5222538000" class="crayon-syntax crayon-theme-github crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-always">
<div class="crayon-toolbar" data-settings=" show">
<div class="crayon-tools">
<div class="crayon-button crayon-nums-button crayon-pressed" title=" 切换是否显示行编号 "></div>
<div class="crayon-button crayon-plain-button" title=" 纯文本显示代码 "></div>
<div class="crayon-button crayon-wrap-button" title=" 切换自动换行 "></div>
<div class="crayon-button crayon-copy-button" title=" 复制代码 "></div>
<div class="crayon-button crayon-popup-button" title=" 在新窗口中显示代码 "></div>
<span class="crayon-language">Python</span></div>
</div>
<div class="crayon-plain-wrap"></div>
<div class="crayon-main">

<div class="crayon-nums-content"> <div class="crayon-num" data-line="crayon-58953244abcc5222538000-1">1</div> <div class="crayon-num crayon-striped-num" data-line="crayon-58953244abcc5222538000-2">2</div> <div class="crayon-num" data-line="crayon-58953244abcc5222538000-3">3</div> <div class="crayon-num crayon-striped-num" data-line="crayon-58953244abcc5222538000-4">4</div> <div class="crayon-num" data-line="crayon-58953244abcc5222538000-5">5</div> <div class="crayon-num crayon-striped-num" data-line="crayon-58953244abcc5222538000-6">6</div> </div> <div class="crayon-pre"> <div id="crayon-58953244abcc5222538000-1" class="crayon-line"><span class="crayon-c"># make empty property</span></div> <div id="crayon-58953244abcc5222538000-2" class="crayon-line crayon-striped-line"><span class="crayon-v">temperature</span> <span class="crayon-o">=</span> <span class="crayon-k ">property</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div> <div id="crayon-58953244abcc5222538000-3" class="crayon-line"><span class="crayon-c"># assign fget</span></div> <div id="crayon-58953244abcc5222538000-4" class="crayon-line crayon-striped-line"><span class="crayon-v">temperature</span> <span class="crayon-o">=</span> <span class="crayon-v">temperature</span><span class="crayon-sy">.</span><span class="crayon-e">getter</span><span class="crayon-sy">(</span><span class="crayon-v">get_temperature</span><span class="crayon-sy">)</span></div> <div id="crayon-58953244abcc5222538000-5" class="crayon-line"><span class="crayon-c"># assign fset</span></div> <div id="crayon-58953244abcc5222538000-6" class="crayon-line crayon-striped-line"><span class="crayon-v">temperature</span> <span class="crayon-o">=</span> <span class="crayon-v">temperature</span><span class="crayon-sy">.</span><span class="crayon-e">setter</span><span class="crayon-sy">(</span><span class="crayon-v">set_temperature</span><span class="crayon-sy">)</span></div> </div>

</div>
</div>
它们之间是相互等价的。