python classmethod,staticmethod,property 一网打尽

废话不多说 看粗糙的代码

 1 # -*- coding:utf-8 -*-
 2 
 3 class A(object):
 4     nn="qwert"
 5     def __init__(self):
 6         self.nn="xoxo"
 7         
 8     def bar(self):
 9         print(self.nn)
10     @classmethod
11     def func(cls):
12         return cls()
13         
14     @staticmethod
15     def foo(name):
16         print("hello"+name+A.nn)
17         
18     @property
19     def score(self):
20         return self._score
21         
22     @score.setter
23     def score(self,value):
24         if not isinstance(value,int):
25             raise ValueError("score must be an integer")
26             
27         if value<0 or value>100:
28             raise ValueError("score must between 0-100!")
29             
30         self._score=value
31         
32         
33         
34 print(A.nn)
35 A().bar()
36 print(A().bar)
37 print(A.func().nn)
38 A.foo("fuck")
39 s=A()
40 s.score=60
41 print(s.score)

打印结果:qwert
xoxo
<bound method A.bar of <__main__.A object at 0x00000239BB88F7B8>>
xoxo
hellofuckqwert
60

======敲一遍打印出来基本就了解了======

猜你喜欢

转载自www.cnblogs.com/lgh-520/p/10212086.html