@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>)
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()
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>
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!