python之@property,@staticmethod,@classmethod

#coding=utf-8

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class MyClass(object):
def __init__(self):
print 'init'
self._name = 'the5fire'
@staticmethod
def static_method():
print 'This is a static method!'
@classmethod
def class_method(cls):
print 'This is a class method',cls
print 'visit the property of the class:',cls.name
print 'visit the static method of the class:',cls.static_method()
instance = cls()
print 'visit the normal method of the class:',instance.test()
def test(self):
print 'call test'
@property
def name(self):
return self._name
if __name__ == '__main__':
MyClass.static_method()
MyClass.class_method()
mc = MyClass()
print mc.name
mc.name = 'huyang'
文章目录