python之函数用法staticmethod

# -*- coding: utf-8 -*-
#python 27
#xiaodeng
#python之函数用法staticmethod
#http://www.cnblogs.com/hongfei/p/3858256.html



#@staticmethod:Return a static method for function.
#函数的静态方法,个人以为尽可能少用该方法
#静态方法在类的内部使用,写在类的定义里面,staticmethod写在函数上方,第一个参数不是self,一般不直接在外部调用,只是在内部使用而已。
class C():
    @staticmethod
    def f(x):
        return x

c=C()
print c.f('xiaodeng')#xiaodeng


#2、没有静态方法的写法如下:
class C():
    def f(self,x):
        return x

c=C()
print c.f('xiaodeng')#xiaodeng

 

posted @ 2015-11-07 16:19  Xiao|Deng  阅读(619)  评论(0编辑  收藏  举报