1、ljust、rjust

"hello".ljust(10,"x")   #将字符串hello做对齐,并且用字符‘x’补到10个字符

#输出为:helloxxxxx

2、repr

def func():
print("hello") repr(func) # 将函数对象转为字符串 # 输出:function func at 0x00000021DC268,如果是print(func)的话自动回调用这个函数。

3、dir(返回对象的属性(成员),函数在python中也是对象,也有属性)def func():    print("hello")

dir(func)  # 返回对象的属性

#输出:['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']

import operator
dir(operator)

#['__abs__', '__add__', '__all__', '__and__', '__builtins__', '__cached__', '__concat__', '__contains__', '__delitem__', '__doc__', '__eq__', '__file__', '__floordiv__', '__ge__', '__getitem__', '__gt__', '__iadd__', '__iand__', '__iconcat__', '__ifloordiv__', '__ilshift__', '__imatmul__', '__imod__', '__imul__', '__index__', '__inv__', '__invert__', '__ior__', '__ipow__', '__irshift__', '__isub__', '__itruediv__', '__ixor__', '__le__', '__loader__', '__lshift__', '__lt__', '__matmul__', '__mod__', '__mul__', '__name__', '__ne__', '__neg__', '__not__', '__or__', '__package__', '__pos__', '__pow__', '__rshift__', '__setitem__', '__spec__', '__sub__', '__truediv__', '__xor__', '_abs', 'abs', 'add', 'and_', 'attrgetter', 'concat', 'contains', 'countOf', 'delitem', 'eq', 'floordiv', 'ge', 'getitem', 'gt', 'iadd', 'iand', 'iconcat', 'ifloordiv', 'ilshift', 'imatmul', 'imod', 'imul', 'index', 'indexOf', 'inv', 'invert', 'ior', 'ipow', 'irshift', 'is_', 'is_not', 'isub', 'itemgetter', 'itruediv', 'ixor',

'le', 'length_hint', 'lshift', 'lt', 'matmul', 'methodcaller', 'mod', 'mul', 'ne', 'neg', 'not_', 'or_', 'pos', 'pow', 'rshift', 'setitem', 'sub', 'truediv', 'truth', 'xor']

# 其中__mul__、__add__是实现细节,mul,add是调用了这个函数。

4、functools-->reduce 迭代运算

from functools import reduce

reduce(lambda a,b:a+b, range(10)) # 计算0~9 数据的和
reduce(lambda a,b:a*b, range(1,10)) #计算1*2*3...*9的积 # reduce(function , iterable_obj),iterable_obj的第一个和第二元素先调用function,得到的结果和第三个元素再调用function,依次类推。

5、operator (C++中也有这些函数对象,替换lambda的作用)

from functools import reduce
import operator reduce(add, range(10)) # 计算0~9 数据的和
reduce(mul, range(1,10)) #计算1*2*3...*9的积

6、operator-->itemgetter(获取可迭代对象的部分数据,多个数据时以元组形式返回)

from operator import itemgetter

getter = itemgetter(1)  # 构造一个获取迭代对象下标为1的可调用对象。
l = [1,2,3,4,5,6] getter(l) # 输出2 getter = itemgetter(2,5)
getter(l) # 输出(3,6)
getter(l)[0] # 输出3

7、operator-->attrgetter(获取对象的部分属性值,多用于提取namedtuple中的部分数据,多个数据以元组形式返回)

from collections import namedtuple
from operator import attrgetter
motro_data = [
('Tokyo', 'JP', 36.933, (35.68, 139.69)),
('Delhi NCR', 'IN', 21.935, (28.61, 77.20)),
('Mexico City', 'MX', 20.142, (19.43, -99.13)),
('New York-Newark', 'US', 20.104, (40.80, -74.02)),
('Sao Paulo', 'BR', 19.649, (-23.54, -46.63))
] LatLong = namedtuple('LatLong', 'lat long') # 定义一个namedtuple类型
Metropolis = namedtuple('Metropolis', 'name cc pop coord') # 定义一个namedtuple类型
metro_areas = [Metropolis(name, cc, pop, LatLong(lat, long)) for name, cc, pop, (lat, long) in motro_data] # 列表推导拆包生成列表,列表的元素是namedtuple
name_lat = attrgetter('name', 'coord.lat') # 构造一个attrgetter对象
for city in sorted(metro_areas, key=attrgetter('coord.lat')): # sorted根据coord.lat值排序,返回一个排序后的列表,元素还是namedtuple
print(name_lat(city)) # 根据name_lat提取city中的name和coord.lat属性打印

8、functools-->partial(绑定函数部分参数,构造新的函数,跟C++的bind函数类似,C++,用_1,_2这些常量来确定绑定那些参数,python可以通过位置参数和关键字参数确定绑定那些参数)

import functools

def func(a,b,c):
print(a,b,c) f = functools.patial(func, 'a', c='c') # 为func函数绑定参数,为a绑定位置实参‘a’,为c绑定关键字实参‘c’
f('hello') # 输出:a hello c # 位置实参只能按照顺序绑定,关键字实参可以绑定任意参数。

python好用的函数或对象的更多相关文章

  1. Python学习笔记之—— File(文件) 对象常用函数

    file 对象使用 open 函数来创建,下表列出了 file 对象常用的函数: 1.file.close() close() 方法用于关闭一个已打开的文件.关闭后的文件不能再进行读写操作, 否则会触 ...

  2. 《流畅的Python》第三部分 把函数视作对象 【一等函数】【使用一等函数实现设计模式】【函数装饰器和闭包】

    第三部分 第5章 一等函数 一等对象 在运行时创建 能赋值给变量或数据结构中的元素 能作为参数传递给函数 能作为函数的返回结果 在Python中,所有函数都是一等对象 函数是对象 函数本身是 func ...

  3. python之attrgetter函数对对象排序

    # 使用attrgetter函数对对象排序 # attrgetter处理对象,itemgetter处理序列 from operator import attrgetter class user(): ...

  4. python学习道路(day4note)(函数,形参实参位置参数匿名参数,匿名函数,高阶函数,镶嵌函数)

    1.函数 2种编程方法 关键词面向对象:华山派 --->> 类----->class面向过程:少林派 -->> 过程--->def 函数式编程:逍遥派 --> ...

  5. python学习之路-day6-面向对象

    一.面向对象学习 特性 class类 一个类即是对一类拥有相同属性的对象的抽象.蓝图.原型.在类中定义了这些对象的都具备的属性.共同的方法. 面向过程编程和面向对象编程: 面向过程编程:使用一系列的指 ...

  6. python中常用的函数与库一

    1, collections.deque 在python里如果我们用列表作为队列使用也是可以的,只是当从队尾删除或者增加元素的时候是很快的,但是从队首删除或者增加元素则要慢得多,这是因为在队首进行操作 ...

  7. python基础-内置函数详解

    一.内置函数(python3.x) 内置参数详解官方文档: https://docs.python.org/3/library/functions.html?highlight=built#ascii ...

  8. python --- Python中的callable 函数

    python --- Python中的callable 函数 转自: http://archive.cnblogs.com/a/1798319/ Python中的callable 函数 callabl ...

  9. python迭代器与iter()函数实例教程

    python迭代器与iter()函数实例教程 发布时间:2014-07-16编辑:脚本学堂 本文介绍了python迭代器与iter()函数的用法,Python 的迭代无缝地支持序列对象,而且它还允许程 ...

随机推荐

  1. GoWeb之gin框架

    Gin 是一个 go 写的 web 框架,具有高性能的优点.官方地址:https://github.com/gin-gonic/gin 一.快速上手 安装 go mod init go get -u ...

  2. less 循环模拟sass的for循环效果

    // 输入框部分宽度 从10px到600px 相隔10像素 .generate-widths(600); .generate-widths(@n, @i: 10) when (@i =< @n) ...

  3. RabbitMQ除开RPC的五种消模型----原生API

    2.五种消息模型 RabbitMQ提供了6种消息模型,但是第6种其实是RPC,并不是MQ,因此不予学习.那么也就剩下5种. 但是其实3.4.5这三种都属于订阅模型,只不过进行路由的方式不同. 通过一个 ...

  4. 《剑指offer》面试题14- II. 剪绳子 II

    问题描述 给你一根长度为 n 的绳子,请把绳子剪成整数长度的 m 段(m.n都是整数,n>1并且m>1),每段绳子的长度记为 k[0],k[1]...k[m] .请问 k[0]*k[1]* ...

  5. 使用Rainbond打包业务模块,实现业务积木式拼装

    背景 每个程序员在学习开发的过程中,都知道解耦和模块化的重要性,也希望自己设计和开发的程序支持模块化,开发好的模块其他人就能快速复用,为了达成这个效果,我们学习各种模块化和解耦的技术,从面向对象的设计 ...

  6. Redisson 实现分布式锁原理分析

    Redisson 实现分布式锁原理分析   写在前面 在了解分布式锁具体实现方案之前,我们应该先思考一下使用分布式锁必须要考虑的一些问题.​ 互斥性:在任意时刻,只能有一个进程持有锁. 防死锁:即使有 ...

  7. golang中的标准库log

    Go语言内置的log包实现了简单的日志服务.本文介绍了标准库log的基本使用. 使用Logger log包定义了Logger类型,该类型提供了一些格式化输出的方法.本包也提供了一个预定义的" ...

  8. JS基础语法(二)

    目录 JavaScript基础语法(二) 八. 函数 1. 函数的概念 2. 函数的使用 声明函数 调用函数 3. 函数的封装 4. 函数的参数 函数的参数匹配问题 5. 函数返回值 6. argum ...

  9. web.xml最新配置

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmln ...

  10. 阿里巴巴基于应用和变更的交付模式|阿里巴巴DevOps实践指南

    编者按:本文源自阿里云云效团队出品的<阿里巴巴DevOps实践指南>,扫描上方二维码或前往:https://developer.aliyun.com/topic/devops,下载完整版电 ...