JAVA网络爬虫
HttpClient

导航

 

python类装饰器相关操作

  • 对象调用自己必须-call方法

    class Test(object):
        def __call__(self):   #类里面定义了这个方法   对象就可以调用自己了
            print("----test1----")
    
    
    t = Test()
    t()  #调用对象自己
    
    '''
        类里面自动调用的方法有
    
        __new__
    
        __init__
    
        __str__
    
        __del__
    
        __call__
    
    '''
    
    # 运行结果
    ----test1----
    
  • 类装饰器

    class Test(object):
        def __init__(self,func):
            print("----初始化----")
            print("func name is %s"%func.__name__)
            self.__func = func
    
        def __call__(self):
            print("----装饰器中的功能----")
            self.__func()
    
    
    
    
    @Test   #等价于   test = Test(test)
    def test():
        print("----test----")
    
    test()
    
    # 运行结果
    ----初始化----
    func name is test
    ----装饰器中的功能----
    ----test----
    
  • 类也是对象

    class Person(object):
        num = 0
        print("----Person----")
        def __init__(self):
            self.name = "laowang"
    
    
    print(1)
    print("haha")
    print(Person)
    
    # 运行结果
    ----Person----
    1
    haha
    <class '__main__.Person'>
    
posted on 2019-08-27 19:43  gmlgxx  阅读(48)  评论(0)    收藏  举报