staticmethod,classmethod

#/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 None

if __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']
>>> 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值