staticmethod,classmethod

来源:互联网 发布:c语言读取字符串 编辑:程序博客网 时间:2024/06/09 15:02
#/usr/bin/python## Author:Tom# Date:2017/04/22# Email:bluelovelydog@gmail.com#class A:    """     this class is test api for python    """    def __init__(self, name, age):        self.name = name        self.age = age    @property    def name(self):        return self.name    @name.setter    def name(self, value):        self.name = value    @property    def name(self):        return self.age    @classmethod    def get_ome(cls, val):        for i in val:            yield i or None    @staticmethod    def get_o(val):        for i in val:            yield i or Noneif __name__ == '__main__':    # a = A(1,2)    # a.name = 10    # print (a.name)    # m = a.get_ome([1,2,3,4,5])    # print(m.next())    # print (m.send(0))    # print (m.send(1))    # test for classmethod    # m = A.get_ome([1,2,3,4,5,6,7,8])    # m.next()    # for i in range(8):    #     try:    #         print (m.send(i))    #     except StopIteration:    #         ValueError(u'u')    # test for classmethod    m = A(1,2)    q = m.get_ome([1,2,3,4])    q.next()    print (q.send(0))    print (q.next())    # test for staticmethod    # n = A.get_o([1,2,3,4])    # n.next()    # print (n.send(0))    # test for staticmethod    # a = A(1,2)    # t = a.get_o([1,2,3,4,5])    # t.next()    # print (t.send(0))

类名不要用数字,否则你无法在shell里import模块

都可以通过实例化类来调用classmethod,staticmethod

也都可以通过类名直接调用classmethod,staticmethod

第一步

>>> dir(a)
['A', '__builtins__', '__doc__', '__file__', '__name__', '__package__']
>>> 

第二步

>>> dir(A)
['__doc__', '__init__', '__module__', 'get_o', 'get_ome', 'name']
>>> 

0 0
原创粉丝点击