如何判断类是类
2018年6月29日 07:29
在写动态加载时,通常需要过滤出需要的类。如何从模块中过滤出其中的类呢?
- 判断a是类A的实例
1 | isinstance(a, A) |
- 判断B类是A的子类
1 | issubclass(B, A) |
- 判断A是一个类
1 | isinstance(A, type ) |
- 判断B是类并且是A的子类
1 | isinstance(B, type ) and issubclass(B, A) |
在写动态加载时,通常需要过滤出需要的类。如何从模块中过滤出其中的类呢?
1 | isinstance(a, A) |
1 | issubclass(B, A) |
1 | isinstance(A, type ) |
1 | isinstance(B, type ) and issubclass(B, A) |
python3中md5加密报错
1 2 3 4 5 6 | >>> import hashlib >>> md5 = hashlib.md5() >>> md5.update( "123" ) Traceback (most recent call last): File "<stdin>" , line 1, in <module> TypeError: Unicode-objects must be encoded before hashing |
正确方式
unicode对象不能直接加密
1 | md5.update( "123" .encode( "utf-8" )) |
1 2 | 1.选中项目根节点 2.File-->Line Separators-->LF Unix |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | def escape_string(value, mapping=None): "" "escape_string escapes *value* but not surround it with quotes. Value should be bytes or unicode. "" " if isinstance(value, unicode): return _escape_unicode(value) assert isinstance(value, (bytes, bytearray)) value = value.replace( '\\' , '\\\\' ) value = value.replace( '\0' , '\\0' ) value = value.replace( '\n' , '\\n' ) value = value.replace( '\r' , '\\r' ) value = value.replace( '\032' , '\\Z' ) value = value.replace( "'" , "\\'" ) value = value.replace( '"' , '\\"' ) return value |
1 2 | # query作为sql模板,args为将要传入的参数 execute(query, args=None) |
1 2 | c:\python36\Scripts\virtualenv.exe test Fatal Python error: Py_Initialize: unable to load the file system codec |
1 | chcp 65001 |
1 2 | >>> "5" .zfill(2) #2表示字符串宽度 '05' |
1 2 3 4 5 6 7 8 | 文件内容 a= "中国" print a #报错 ➜ ~ python a.py File "a.py" , line 1 SyntaxError: Non-ASCII character '\xe4' in file b.py on line 1, but no encoding declared; see http: //python .org /dev/peps/pep-0263/ for details |
1 2 3 4 5 6 7 8 9 10 | >>> sys.getdefaultencoding() 'ascii' >>> a= "中国" >>> a.encode( "gbk" ) Traceback (most recent call last): File "<stdin>" , line 1, in <module> UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128) >>> b=u "中国" >>> b.encode( "gbk" ) '\xd6\xd0\xb9\xfa' |
1 2 3 4 5 6 7 8 | >>> "中国" .decode( "utf-8" ).encode( "ascii" ) Traceback (most recent call last): File "<stdin>" , line 1, in <module> UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128) >>> "中国" .decode( "utf-8" ).encode( "gbk" ) '\xd6\xd0\xb9\xfa' >>> "a" .decode( "utf-8" ).encode( "ascii" ) 'a' |
1 2 3 4 5 6 7 8 9 10 | >>> a = "中国" >>> b = a.decode( "utf-8" ) >>> b u '\u4e2d\u56fd' >>> b.decode( "utf-8" ) Traceback (most recent call last): File "<stdin>" , line 1, in <module> File "/usr/lib64/python2.7/encodings/utf_8.py" , line 16, in decode return codecs.utf_8_decode(input, errors, True) UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128) |
1 2 3 4 | >>> sys.getsizeof( "a" ) #pyton做了包装,包含了其它数据内容 38 >>> sys.getsizeof( "aa" ) - sys.getsizeof( "a" ) # 将字符重复一个求差值,可知单个字符长度 1 |
1 2 3 4 | >>> sys.getsizeof(u "aa" ) - sys.getsizeof(u "a" ) #python2中4个字节、python3中2个字节 4 >>> sys.getsizeof(u "你你" ) - sys.getsizeof(u "你" ) 4 |
1 2 3 4 5 6 7 | >>> a1,a2= "a" .encode( "utf-8" ), "aa" .encode( "utf-8" ) >>> sys.getsizeof(a2) - sys.getsizeof(a1) #英文一个字节 1 >>> b1,b2= "你" .decode( "utf-8" ).encode( "utf-8" ), "你你" .decode( "utf-8" ).encode( "utf-8" ) >>> sys.getsizeof(b2) - sys.getsizeof(b1) #中文三个字节 3 |
1 2 3 4 5 6 7 8 9 | ➜ ~ python3 >>> bytes( "你" , "gbk" ) b '\xc4\xe3' >>> bytes( "a" , "gbk" ) b 'a' >>> bytes( "你" , "utf-8" ) b '\xe4\xbd\xa0' >>> bytes( "a" , "utf-8" ) b 'a' |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 英文字母: 字节数 : 1;编码:GB2312 字节数 : 1;编码:GBK 字节数 : 1;编码:GB18030 字节数 : 1;编码:ISO-8859-1(latin-1) 字节数 : 1;编码:UTF-8 字节数 : 4;编码:UTF-16 字节数 : 2;编码:UTF-16BE 字节数 : 2;编码:UTF-16LE 中文汉字: 字节数 : 2;编码:GB2312 字节数 : 2;编码:GBK 字节数 : 2;编码:GB18030 字节数 : 1;编码:ISO-8859-1(latin-1) 字节数 : 3;编码:UTF-8 字节数 : 4;编码:UTF-16 字节数 : 2;编码:UTF-16BE 字节数 : 2;编码:UTF-16LE |
1 2 3 4 5 6 7 8 9 | def f(a, L = []): L.append(a) return L for i in range ( 3 ): print f(i) #输出 [ 0 ] [ 0 , 1 ] [ 0 , 1 , 2 ] |
1 2 3 4 | def f(a, t = None ): t = t or [] t.append(a) return t |