Home > Backend Development > Python Tutorial > Python Day 33——Static methods, class methods, attribute methods

Python Day 33——Static methods, class methods, attribute methods

PHP中文网
Release: 2017-06-19 10:44:08
Original
1110 people have browsed it

@staticmethod After decoration, the methods in the class are converted into static methods

<span style="color: #008080">1</span> <span style="color: #0000ff">class</span><span style="color: #000000"> a:
</span><span style="color: #008080">2</span>     
<span style="color: #008080">3</span> <span style="color: #000000">     @staticmethod
</span><span style="color: #008080">4</span>      <span style="color: #0000ff">def</span><span style="color: #000000"> b(self):
</span><span style="color: #008080">5</span>         <span style="color: #0000ff">print</span>(<span style="color: #800000">''</span>)
Copy after login

Static methods cannot access instance variables or class variables, which are equivalent to toolkits in classes. Such as os, system and other imported modules are generally

After @classmethod decoration, the methods in the class are converted into class methods. The difference between class methods and ordinary methods is that class methods can only access class variables and cannot access instance variables

<span style="color: #008080">1</span> <span style="color: #0000ff">class</span><span style="color: #000000"> b(object):
</span><span style="color: #008080">2</span>     name=<span style="color: #800000">'</span><span style="color: #800000">aa</span><span style="color: #800000">'</span>
<span style="color: #008080">3</span> <span style="color: #000000">    @classmethod
</span><span style="color: #008080">4</span>     <span style="color: #0000ff">def</span><span style="color: #000000"> cc(self):
</span><span style="color: #008080">5</span>         <span style="color: #0000ff">print</span>(<span style="color: #800000">'</span><span style="color: #800000">%s .l.....</span><span style="color: #800000">'</span>%<span style="color: #000000">name)
</span><span style="color: #008080">6</span> 
<span style="color: #008080">7</span> 
<span style="color: #008080">8</span> 
<span style="color: #008080">9</span> b.cc()
Copy after login

After decorating @property, convert the methods in the class into static properties

How to use static attributes

<span style="color: #008080"> 1</span> <span style="color: #0000ff">class</span><span style="color: #000000"> Eat_food(object):
</span><span style="color: #008080"> 2</span>     self.<span style="color: #800080">__food</span>=None<span style="color: #008000">#</span><span style="color: #008000">设置一个私有属性</span>
<span style="color: #008080"> 3</span>     
<span style="color: #008080"> 4</span> <span style="color: #000000">    @property
</span><span style="color: #008080"> 5</span>     <span style="color: #0000ff">def</span><span style="color: #000000"> eat(self):
</span><span style="color: #008080"> 6</span>         <span style="color: #0000ff">print</span>(<span style="color: #800000">'</span><span style="color: #800000">.....%s</span><span style="color: #800000">'</span>%self.<span style="color: #800080">__food</span><span style="color: #000000">)
</span><span style="color: #008080"> 7</span>     
<span style="color: #008080"> 8</span>     @eat.setter  <span style="color: #008000">#</span><span style="color: #008000">再次装饰后可以对其赋值</span>
<span style="color: #008080"> 9</span>     <span style="color: #0000ff">def</span><span style="color: #000000"> eat(self,food):
</span><span style="color: #008080">10</span>         <span style="color: #0000ff">print</span>(<span style="color: #800000">'</span><span style="color: #800000">.....%s</span><span style="color: #800000">'</span>%<span style="color: #000000">food)
</span><span style="color: #008080">11</span>         self.<span style="color: #800080">__food</span>=food<span style="color: #008000">#</span><span style="color: #008000">保存到私有属性,备用</span>
<span style="color: #008080">12</span>     
<span style="color: #008080">13</span> <span style="color: #000000">    @eat.deleter
</span><span style="color: #008080">14</span>     <span style="color: #0000ff">def</span><span style="color: #000000"> eat(self):
</span><span style="color: #008080">15</span>         <span style="color: #0000ff">del</span> self.<span style="color: #800080">__food</span><span style="color: #008000">#</span><span style="color: #008000">删除保存赋值的私有属性,相当于删除这个属性方法</span>
Copy after login

The above is the detailed content of Python Day 33——Static methods, class methods, attribute methods. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template