zoukankan      html  css  js  c++  java
  • 一些常用函数

    1、isinstance()

    isinstance(object,type)

    object,为具体的对象,type为类名,也可是类名组成的列表。

    (1)type为类名时,object为类type的一个具体事列则返回True

    (2)type为列表时,当object满足type列表中的一个具体类名时,则返回True,否则返回False

    如:

    class MyClass(object):
        def __init__(self):
            pass
    h = MyClass()
    print(isinstance(h,MyClass))//True

     2、property函数,在Django源码中的一个用法

        def _get_post(self):
            if not hasattr(self, '_post'):
                self._load_post_and_files()
            return self._post
    
        def _set_post(self, post):
            self._post = post
    
        POST = property(_get_post, _set_post)
    _get_post:获取post的值
    _set_post:设置post的值
    上述最后一句代码实现,.POST获取post值,.POST = ''设置post的值。
    class property():
        def __init__(self, fget=None, fset=None, fdel=None, doc=None)
    
    class property([fget[, fset[, fdel[, doc]]]])

    4个参数分别为:

    fget:获取属性值的函数
    fset:设置属性值的函数
    fdel:删除属性值的函数
    doc:属性描述信息
    class MyClass(object):
        def __init__(self):
            self.x = 0
    
        def get_x(self):
            return self.x
    
        def set_x(self,value):
            self.x = int(value)
    
        def del_x(self):
            del self.x
        X = property(get_x,set_x,del_x,"xxx")
    
    
    h = MyClass()
    print(h.x)//触发get_x的执行
    h.x = 7//触发set_x的执行
    print(h.x)
    del h.x//触发del_x的执行

     3、map函数

    map函数用法:

    num_list = [1,2,3,4,5]
    print(list(map(lambda x:x**2,num_list)))

    自己实现map函数:

    def map_test(func,array):
        """
        :param func: 传入进来的函数地址,处理逻辑,如果处理逻辑简单,使用lamda,处理逻辑难,自己定义函数
        :param array: 需要处理的数组,可迭代对象
        :return: 处理后的数组
        """
        l = []
        for i in array:
            l.append(func(i))
        return l
    print(map_test(lambda x:x**2,num_list))

    结果合上述内置的map函数一样,只是map函数的返回值为map对象,为迭代器,迭代了一次之后map返回值中就没有数据了。

    string = "hello"
    print(list(map(lambda x:x.upper(),string)))
    # 将字符串转换为大写

    4、匿名函数

    # 匿名函数
    # lambda 形参:返回值
    print(lambda x:x+1)
    # 结果:<function <lambda> at 0x00000000004E2F28>,为函数的地址
    func = lambda x:x+1
    print(func(11))
    # 12
    # 匿名函数实现相当于如下函数,有输入,输出,只是类比
    def add_one(x):# 输入
        return x+1# 输出

    匿名函数用于处理简单逻辑,形参可以多个,返回值可以为元组。

    5、filter函数(过滤函数)

    string = ['abc','abd','ccc','abe','cab','dba','dab']
    
    print(list(filter(lambda x:x.startswith('ab'),string)))#找出以ab开头的
    print(list(filter(lambda x:x.endswith('ab'),string)))#找出以ab结尾的

    自己实现filter函数:

    def filter_test(func,array):
        ret = []
        for i in array:
            if func(i):
                ret.append(i)
        return ret
    print(filter_test(lambda x:x.startswith("ab"),string))
    print(filter_test(lambda x:x.endswith("ab"),string))

    这个实现结果合上述一样,内置filter函数返回的是迭代器对象。

    6、reduce函数

    对可迭代对象和侦破那个的元素实现累加或者累乘。

    from functools import reduce
    num_list = [1,2,3,4,5]
    print(reduce(lambda x,y:x+y,num_list))

    自己实现reduce函数:

    num_list = [1,2,3,4,5]
    
    
    def reduce_test(func,array):
        ret = array.pop(0)
        for i in array:
            ret = func(ret,i)
        return ret
    
    print(reduce_test(lambda x,y:x+y,num_list))
  • 相关阅读:
    Kubernetes stateful set讲解以及一个基于postgreSQL的具体例子
    如何在Kubernetes里给PostgreSQL创建secret
    如何使用Kubernetes的configmap通过环境变量注入到pod里
    使用Gardener在Google Cloud Platform上创建Kubernetes集群
    通过describe命令学习Kubernetes的pod属性详解
    使用describe命令进行Kubernetes pod错误排查
    一个简单的例子理解Kubernetes的三种IP地址类型
    不同编程语言在发生stackoverflow之前支持的调用栈最大嵌套层数
    (十)golang--运算符
    (九)golang--标识符的命名规则
  • 原文地址:https://www.cnblogs.com/zjsthunder/p/9772196.html
Copyright © 2011-2022 走看看