python装饰器是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的有插入日志、性能测试、事务处理等。装饰器是解决这类问题的绝佳设计,有了装饰器,我们就可以抽离出大量函数中与函数功能本身无关的雷同代码并继续重用。概括的讲,装饰器的作用就是为已经存在的对象添加额外的功能。
分类:py_basic
python的getattr(),setattr(),delattr(),hasattr()
getattr(),setattr(),delattr(),hasattr()为常用的对象属性处理内置函数。
继续阅读“python的getattr(),setattr(),delattr(),hasattr()”
set–列表求交集、并集、差集
python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和sysmmetric difference(对称差集)等数学运算。sets 支持 x in set, len(set),和 for x in set。作为一个无序的集合,sets不记录元素位置或者插入点。因此,sets不支持 indexing, slicing, 或其它类序列(sequence-like)的操作。 继续阅读“set–列表求交集、并集、差集”
判断是否为Nonetype
报错:
1 2 |
print type(a) None <type 'NoneType'> |
python 如何判断 是nonetype
1 2 |
if a is None: print "dddddddddddd" |
python打印出特殊符号
1 |
三角形Δ print '%s' % u'\u0394' |
ljust rjust center 对齐输出
Python中打印字符串时可以调用ljust(左对齐),rjust(右对齐),center(中间对齐)来输出整齐美观的字符串,使用起来非常简单,包括使用第二个参数填充(默认为空格)。 继续阅读“ljust rjust center 对齐输出”
enumerate函数–遍历元素以及下标
enumerate 函数用于遍历序列中的元素以及它们的下标
python文档中是这么说的:
1 2 3 4 5 6 |
enumerate(sequence, [start=0]) Return an enumerate object. sequence must be a sequence, an iterator, or some other object which sup- ports iteration. The next() method of the iterator returned by enumerate() returns a tuple containing a count (from start which defaults to 0) and the corresponding value obtained from iterating over iter- able. enumerate() is useful for obtaining an indexed series: (0, seq[0]), (1, seq[1]), (2, seq[2]), .... |
8种常见的排序方法以及python的实现
小辉在朋友圈里分享的python常见的排序方法,真是条条道路通罗马啊
python异常处理
在N多个数据一起处理的时候,如果某一个数据处理过程中出现了问题,那么我又不希望其他的停止,这个时候就需要异常处理
python里splitlines函数
Python的split方法函数可以分割字符串成列表,默认是以空格作为分隔符sep来分割字符串。那么行的分割呢? 继续阅读“python里splitlines函数”