python-自定义@修饰符

__new__函数
在实例化开始之后,在调用 __init__() 方法之前,Python 首先调用 __new__() 方法 
#单例1
class Singleton1(object):
    def __new__(cls,*args, **kwargs):
        if not hasattr(cls,'_inst'):
            cls._inst = super(Singleton1, cls).__new__(cls) #相当于object.__new__(cls)
        return cls._inst

#单例2
class Singleton2(object):
    def __new__(cls,*args, **kwargs):
        if not hasattr(cls,'_inst'):
            cls._inst = object.__new__(cls)
        return cls._inst

if __name__ == '__main__':
    print(Singleton1())
    print(Singleton1())
    print(Singleton2())
    print(Singleton2())
class Person(object):
    #自定义单列
    def __new__(cls, name, age):
        if 0 < age < 150:
            return super(Person, cls).__new__(cls)  # return object.__new__(cls)
        else:
            return None

    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __str__(self):
        return f'{self.__class__.__name__}{self.__dict__}'


print(Person('Tom', 10))
import time
time.sleep(5)
print(Person('Mike', 200))

@staticmethod、@classmethod修饰符

我们知道对于一个普通的类,我们要使用其中的函数的话,需要对类进行实例化,而一个类中,某个函数前面加上了staticmethod或者classmethod的话,那么这个函数就可以不通过实例化直接调用

@staticmethod不需要表示自身对象的self和自身类的cls参数,就跟使用函数一样。
  如果在@staticmethod中要调用到这个类的一些属性方法,只能直接类名.属性名或类名.方法名。
@classmethod也不需要self参数,但第一个参数需要是表示自身类的cls参数。
  而@classmethod因为持有cls参数,可以来调用类的属性,类的方法,实例化对象等,避免硬编码。

class Animal(object):
    name = 'dog'
    def __init__(self,name):
        self.name = name

    def intro1(self):
        print('there is a %s'%(self.name))

    @staticmethod
    def intro2():
        print('there is a %s')

    @classmethod
    def intro3(cls):
        print('there is a %s'%(cls.name))

Animal('cat').intro1()
Animal.intro2()
Animal.intro3()
View Code

@property修饰符

property使方法像属性一样调用,就像是一种特殊的属性

有参函数时,@name.setter

class Animal(object):
    def __init__(self,name):
        self.name = name
    @property
    def intro(self):
        print('there is a %s eating'%(self.name))

    @intro.setter
    def intro(self,value):
        print('there is %d %s eating'%(value,self.name))

a = Animal('cat')
a.intro
a.intro=2
View Code

@修饰符

从第一个函数修饰符开始,自下而上做参数传递

#修饰无参函数
def log1(func):
    func()
@log1
def test():
    print('test:')


#修饰有参函数
def log2(func):
    def inner(*args, **kwargs):
        func(*args, **kwargs)
    return inner
@log2
def test(num):
    print('testlog2:',num,test.__name__)
test(20) #相当于log(test(20))


from functools import wraps
#@wraps不改变使用装饰器原有函数的结构(如name, doc)
#修饰有参函数
def log3(func):
    @wraps(func)
    def inner(*args, **kwargs,):
        func(*args, **kwargs)
    return inner
@log3
def test(num):
    print('testlog3:',num,test.__name__)
test(30) #相当于log(test(30))
View Code

@pysnooper修饰符

日志打印工具,用显示函数间的入参和返回值的变化

import pysnooper

@pysnooper.snoop()
#@pysnooper.snoop('log.log')
def lisy(a):
    b=[x - 10 if x in [11, 12, 13] else x for x in a]
    return b
print(lisy([1,2,3,11,12,13,'111',222]))
View Code

原文地址:https://www.cnblogs.com/shuzf/p/11649339.html