Python内置函数

目录

1、format() 

2、super() 函数

3、isinstance() 函数

4、filter()函数

5、map()函数

6、reduce()函数


1、format() 

# 一、通过元组设置参数
# 1.不设置指定位置,按默认顺序
"{} {}".format("hello", "world")
# 2.设置指定位置
"{1} {0} {1}".format("hello", "world")  

# 二、通过关键字设置参数
"网站名:{name}, 地址 {url}".format(name="菜鸟教程",url="www.runoob.com")

# 三、通过字典设置参数
site = {"name": "菜鸟教程", "url": "www.runoob.com"}
"网站名:{name}, 地址 {url}".format(**site)

# 四、通过列表索引设置参数
my_list = ['菜鸟教程', 'www.runoob.com']
"网站名:{0[0]}, 地址 {0[1]}".format(my_list))  # "0" 是必须的

# 五、通过class对象设置参数
class AssignValue(object):
    def __init__(self, value):
         self.value = value
my_value = AssignValue(6)
'value 为: {0.value}'.format(my_value) # "0" 是可选的,value是实例属性

# 六、数字格式化
":.2f".format(3.1415926)

2、super() 函数

super()函数可以调用父类(超类)的一个方法。用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是如果使用多继承,会涉及到查找顺序(MRO)、重复调用(钻石继承)等种种问题。语法如下:

super(type[, object-or-type])

参数说明:

  • type -- 类。
  • object-or-type -- 类,一般是 self

Python3.x 和 Python2.x 的一个区别是: Python 3 可以使用直接使用 super().xxx 代替 super(Class, self).xxx 

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
class FooParent(object):
    def __init__(self):
        self.parent = 'I\'m the parent.'
        print ('Parent')
    
    def bar(self,message):
        print ("%s from Parent" % message)
 
class FooChild(FooParent):
    def __init__(self):
        # super(FooChild,self) 首先找到 FooChild 的父类(就是类 FooParent),然后把类 FooChild 的对象转换为类 FooParent 的对象
        super(FooChild,self).__init__()    
        print ('Child')
        
    def bar(self,message):
        super(FooChild, self).bar(message)
        print ('Child bar fuction')
        print (self.parent)
 
if __name__ == '__main__':
    fooChild = FooChild()
    fooChild.bar('HelloWorld')

执行结果:

Parent
Child
HelloWorld from Parent
Child bar fuction
I'm the parent.

3、isinstance() 函数

isinstance() 函数来判断一个对象是否是一个已知的类型,类似 type(),如果要判断两个类型是否相同推荐使用 isinstance()

isinstance() 与 type() 区别:

  • type() 不会认为子类是一种父类类型,不考虑继承关系
  • isinstance() 会认为子类是一种父类类型,考虑继承关系
class A:
    pass
 
class B(A):
    pass
 
isinstance(A(), A)    # returns True
type(A()) == A        # returns True
isinstance(B(), A)    # returns True
type(B()) == A        # returns False

4、filter()函数

对可遍历的对象进行过滤,适合进行字符串处理。如下:该函数可以对字符串进行处理。返回一个列表的对象。

num_list = ["我","是","哈哈","太平洋海工欢唱","六队船厂","六队码头","六队船坞"]
def filter_list(func,array):
    res = []
    for i in array:
        if func(i):
            res.append(i)
    return res
print(list(filter_list(lambda x:x.startswith("六队"),num_list)))
print(list(filter(lambda x:x.startswith("六队"),num_list)))
# 输出
['六队船厂', '六队码头', '六队船坞']
['六队船厂', '六队码头', '六队船坞']

5、map()函数

将一个列表进行遍历,对每一个字符串进行处理,如下:对可迭代的对象进行单个处理。

num_list = ["我","是","哈哈","太平洋海工欢唱","六队船厂","六队码头","六队船坞"]
def test(func, x):
    new_list = []
    for i in x:
        new_list.append(func(i))
    return new_list
 
print(list(test(lambda x:str(x)+"叠加",num_list)))
print(list(map(lambda x:str(x)+"叠加",num_list)))
# 输出
['我叠加', '是叠加', '哈哈叠加', '太平洋海工欢唱叠加', '六队船厂叠加', '六队码头叠加', '六队船坞叠加']
['我叠加', '是叠加', '哈哈叠加', '太平洋海工欢唱叠加', '六队船厂叠加', '六队码头叠加', '六队船坞叠加']

6、reduce()函数

该函数对整数进行处理。加减乘除都可以。

from functools import reduce
a = [1, 2, 3, 100]
 
def num(array, func,init = None):
    if init == None:
        res = array.pop(0)
    else:
        res = init
    for i in array:
        res = func(res, i)
    return res
print(reduce(lambda x,y:x*y,a,100))
print(num(lambda x,y:x*y,a,100))

# 输出
60000
60000

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值