随笔分类 - Python语法
摘要:>>> class A(object): bar = 1 def func1(self): print 'foo' >>> class A(object): bar = 1 def func1(self): print 'foo' @classmethod def func2(cls): ...
阅读全文
摘要:class Test(object): def __init__(self, value='hello, world!'): self.data = value >>> t = Test() >>> t >>> print t # 看到了么?上面打印类对象并不是很友好,显示的是对象的内存地址 # 下面我们重构下该类的__repr__以及__str__,看看它们俩有...
阅读全文
摘要:>>> from timeit import Timer >>> Timer("temp = x; x = y; y = temp", "x = 2; y = 3").timeit() 0.04535215532074004 >>> Timer("x, y = y, x", "x = 2; y = 3").timeit() 0.06476211600816484
阅读全文
摘要:>>> x = 1 >>> y = 2 >>> assert x == y, "not equals" Traceback (most recent call last): File "", line 1, in assert x == y, "not equals" AssertionError: not equals >>>
阅读全文
摘要:#coding=utf-8 #python 多线程 #原函数 def execute_slowly(a, b, c): pass #多线程 from threading import Thread t = Thread(target=execute_slowly, args=(a, b, c)) t.start()
阅读全文
摘要:#coding=utf-8 class YouNeedError(Exception): pass try: raise YouNeedError("lola") except Exception as e: print str(e)
阅读全文
摘要:捕获异常的两种方式 方法一 #coding=utf-8 import sys try: with open("ddd.txt", "r") as f: data = f.read() print data except: err = sys.exc_info() print err sys.exc_info()返回三元组,分别是,异常...
阅读全文
摘要:1、建立发布文件夹,内涵README.txt, setup.py vsearch.py(自写程序) 其中 setup.py如下 #coding=utf-8 from setuptools import setup setup( name = "vsearch", version = "1.0", description = "The head first Python...
阅读全文
摘要:#coding=utf-8 m = {"a", "b", "c", "d"} n = {"d", "e", "f", "b"} #并集 p = m.union(n) print p #哪些不是共有元素 q = m.difference(n) print q #查找共同元素 r = m.intersection(n) print r
阅读全文
摘要:python导出安装包及版本 pip freeze > requirements.txt批量安装pip install -r requirements.txt
阅读全文
摘要:来源:https://www.cnblogs.com/nancyzhu/p/8551506.html日志 日志是跟踪软件运行时所发生的事件的一种方法。软件开发者在代码中调用日志函数,表明发生了特定的事件。事件由描述性消息描述,该描述性消息可以可选地包含可变数据(即,对于事件的每次出现都潜在地不同的数据)。事件还具有开发者归因于事件的重要性;重要性也可以称为级别或严重性。 logging提供了一...
阅读全文
摘要:来源:https://www.cnblogs.com/plwang1990/p/3757549.html 今天写程序又记不清格式化输出细节了……= =索性整理一下。 python print格式化输出。 1. 打印字符串 print ("His name is %s"%("Aviad")) 效果:
阅读全文
摘要:在使用json.dumps时要注意一个问题 >>> import json >>> print json.dumps('中国') "\u4e2d\u56fd" 输出的会是 '中国' 中的ascii 字符码,而不是真正的中文。 这是因为json.dumps 序列化时对中文默认使用的ascii编码.想输
阅读全文
摘要:来源:https://www.cnblogs.com/aland-1415/p/6613449.html Python中 sys.argv[]的用法简明解释 因为是看书自学的python,开始后不久就遇到了这个引入的模块函数,且一直在IDLE上编辑了后运行,试图从结果发现它的用途,然而结果一直都是没
阅读全文
摘要:"TypeError: 'NoneType' object is not iterable" 一般是返回值为None同时赋值给了多个变量
阅读全文
摘要:#coding:utf-8 import urllib legal_person_string = "%E6%B3%95%E5%AE%9A%E4%BB%A3%E8%A1%A8%E4%BA%BA" legal_person_string = legal_person_string.decode("gbk").encode('utf-8') legal_person_caption = urllib...
阅读全文
摘要:python __file__ 与argv[0] 在python下,获取当前执行主脚本的方法有两个:sys.argv[0]和__file__。 sys.argv[0] 获取主执行文件路径的最佳方法是用sys.argv[0],它可能是一个相对路径,所以再取一下abspath是保险的做法,像这样: import os,sys dirname, filename = os.path.split(o...
阅读全文
摘要:1 >>> dict = {"name":"zara", "age": 7} 2 >>> dict2 = {"sex":"female"} 3 >>> dict.update(dict2) 4 >>> dict 5 {'name': 'zara', 'age': 7, 'sex': 'female'}
阅读全文
摘要:# 单元测试 如果你听说过“测试驱动开发”(TDD:Test-Driven Development),单元测试就不陌生。 单元测试是用来对一个模块、一盒函数或者一个类来进行正确性检验的测试工作。 比如对函数abs(),我们可以编写出以下几个测试用例: 1、输入整数,比如1、1.2、0.99,期待返回值与输入相同; 2、输入负数,比如-1、-1.2、-0.99,期待返回值与是呼入相反; 3、输...
阅读全文
摘要:调试 程序能一次写完并正常执行的概率很小。总会有各种各样的bug需要修正。 有的bug很简单,看看错误信息就知道,有的bug很复杂,我们需要知道出错时 哪些变量的值是正确的,哪些变量的值是错误的,因此,需要一整套调试程序的手段来修复bug。 第一种方法:print 简单直接粗暴有效,就是用print()把可能有问题的变量打印出来看看: def foo(s): n = int(s) ...
阅读全文