![](https://img-blog.csdnimg.cn/20201014180756738.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
python
生命长跑
这个作者很懒,什么都没留下…
展开
-
python函数形参中的*args和**kwargs
在说明*args和**kwargs参数之前先说一下其他函数参数:一、位置参数:def power(x): return x * x对于power(x)函数,参数x就是一个位置参数。二、默认参数:def power(x, n=2): s = 1 while n > 0: n = n - 1 s = s * x ...原创 2018-09-05 10:02:10 · 936 阅读 · 0 评论 -
Ubuntu下通过virtualenv创建虚拟环境,配置镜像源
一、创建指定python版本的虚拟环境进入到待创建虚拟环境的保存目录(这里我们创建到/home/下):cd /home/创建python2.7环境,命名为py2env:virtualenv py2env --python=python2.7此时/home/下就会有一个py2env文件夹二、激活虚拟环境激活虚拟环境source py2env/bin/activ...原创 2019-07-23 11:15:24 · 466 阅读 · 0 评论 -
python中利用线程池提高for循环效率
任务:读取文件每一行,并执行相应的操作(以打印行内容为例)。for循环方式:f = open('test.txt', 'r')for line in f.readlines(): print(line)f.close()线程池方式:from multiprocessing.pool import ThreadPooldef my_print(item): ...原创 2019-07-24 17:12:57 · 17959 阅读 · 1 评论 -
python中的编码问题及读写中文(codecs模块、文件读写)
问题1:之前一直使用python自带的open()函数读写文件没什么问题,最近项目需求需要操作文件读写中文,遇到了编码问题。解决思路:codecs模块用于各种编码之间的转换,在打开文件时指定编码格式即可。向文件中写入中文:f = codecs.open('filename', 'w', 'utf-8')f.write('中文')f.close()问题2:列表中包含中文...原创 2019-07-15 10:12:05 · 1061 阅读 · 0 评论 -
python中的可变对象和不可变对象、浅拷贝和深拷贝(涉及copy模块)
初学python你可能会遇到以下问题(小白一枚的我反正是遇到了^-^&),话不多说上代码:a = [1, 2]b = ab[0] = 3print(a)print(b)# 执行结果:[3,2][3,2]咦~,什么鬼,我明明修改的b,怎么连着a也一起变了??不要慌,我来告诉你超究极奥义:python一切皆对象!!不贫了,上正题:python一切皆对象,对象...原创 2019-07-25 12:19:55 · 389 阅读 · 0 评论 -
Python读写json文件和pickle文件
json和pickle很相近,接口都是load、loads、dump、dumps。区别在于pickle是专门为python设计的,因此它支持python中所有的数据类型。读json文件并将序列化字符串反序列化:json.load() # 返回一个字典with open('test.json', 'r') as f: dic = json.load(f)将对象序列化并保存...原创 2019-06-18 16:55:31 · 1310 阅读 · 0 评论 -
Python中常见的文件、目录操作(os、codecs、shutil、glob模块)
一、创建、删除、重命名、复制涉及到的模块:os、codecs和shutil创建目录:os.mkdir('directory') # 创建一个目录,命名为directory重命名目录(文件):os.rename('oldname', 'newname')移动目录(文件):shutil.move('old', 'new')复制目录:shutil.copytree(...原创 2019-06-18 16:32:43 · 383 阅读 · 0 评论 -
python中的isinstance()和type()区别
先看一段代码:from easydict import EasyDict as edictdef is_dict(input): if type(input) is dict: return True else: return Falsedef is_dict_1(input): if isinstance(input, dict)...原创 2019-09-10 14:18:31 · 672 阅读 · 0 评论 -
python中的特殊函数__init__()和__call__()
在python中,__int__()函数的意义等同于类的构造器(同理,__del__()等同于类的析构函数)。因此,__init__()方法的作用是创建一个类的实例。class Flower(object): def __init__(self,color): self.color = color print('color is', color)r...原创 2018-09-03 18:24:03 · 1295 阅读 · 0 评论 -
python中的__str__和__repr__
如果要把一个类的实例变成str,就需要实现特殊方法__str__()和__repr__()(可选项).Python 中定义了__str__()和__repr__()两种方法,__str__()用于显示给用户,而__repr__()用于显示给开发人员。具体区别见代码:class Person(object): def __init__(self, name, age): ...原创 2019-09-18 19:46:15 · 886 阅读 · 0 评论