Python基础--Pickle/函数默认参数/函数的参数*args/Bytes<=;str/32-64bit/bytes对象
Posted 吴英强
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python基础--Pickle/函数默认参数/函数的参数*args/Bytes<=;str/32-64bit/bytes对象相关的知识,希望对你有一定的参考价值。
Pickle
>>> import pickle
>>> my_list = [1, 2, 3, 'haha', ['and', 'or']]
>>> pickle_file = open('my_list.pkl', 'wb')
>>> pickle.dump(my_list, pickle_file)
>>> pickle_file.close()
>>>
>>>
>>> pickle_file = open('my_list.pkl', 'rb')
>>> my_list2 = pickle.load(pickle_file)
>>> print(my_list2)
[1, 2, 3, 'haha', ['and', 'or']]
>>>
函数默认参数
>>> def sayWord(name = 'wuyq', word = '快乐学python'):
print(name + '-->' + word)
>>> sayWord()
wuyq-->快乐学python
>>> sayWord('吴英强', '坚持学习,锻炼身体')
吴英强-->坚持学习,锻炼身体
>>> def test(*params):
print('参数的长度是:',len(params))
print('第二个参数是:', params[1])
>>> test(1, 2, 3, 4)
参数的长度是: 4
第二个参数是: 2
>>>
函数的参数*args
#coding=utf8
__author__ = 'wuyq'
#当函数的参数不确定时
#*args可以当作可容纳多个变量组成的list或tuple
def fun_var_args(farg, *args):
print('args: %s'% farg)
for value in args:
print('another arg:%s'% value)
fun_var_args(1, 'two', 3, None)
函数的参数**kwargs
def fun_var_kwargs(farg, **kwargs):
print("args:%s"% farg)
for key in kwargs:
print("another keyword arg:%s:%s" %(key, kwargs[key]))
fun_var_kwargs(1, myarg1='two', myarg2=3, myarg3=None)
Bytes<=>str
str对象和bytes对象可以使用.encode() (str -> bytes) or .decode() (bytes -> str)方法相互转化。
>>> b = b'china'
>>> b
b'china'
>>> type(b)
<class 'bytes'>
>>>
>>> s = b.decode()
>>> s
'china'
>>> bl = s.encode()
>>> bl
b'china'
>>>
32-64bit
#判断平台是64还是32位
import sys
bits = 0
v = sys.maxsize
while v:
bits += 1
v >>= 1
if bits > 32:
print("64bit")
else:
print("32bit")
#字节序
print(sys.byteorder)
bytes对象
>>> by = b"abcde"
>>> by
b'abcde'
>>> barr = bytearray(by)
>>> barr
bytearray(b'abcde')
>>> barr[0]
97
>>> barr[0] = 98
>>> barr
bytearray(b'bbcde')
>>>
>>> by = b"abcde"
>>> len(by)
5
>>> by += b"f"
>>> by
b'abcdef'
>>> by[0]
97
>>> by[0] = 98
Traceback (most recent call last):
File "<pyshell#68>", line 1, in <module>
by[0] = 98
TypeError: 'bytes' object does not support item assignment
>>>
不能混用bytes和strings
不能连接bytes对象和字符串。他们两种不同的数据类型。
也不允许针对字符串中bytes对象的出现次数进行计数,因为串里面根本没有bytes。字符串是一系列的字符序列。也许你是想要先把这些字节序列通过某种编码方式进行解码获得字符串,需要显式地指明它。Python 3不会隐含地将bytes转换成字符串,或者进行相反的操作。
>>> by = b"d"
>>> s = "abcde"
>>> by
b'd'
>>> s
'abcde'
>>> by + s
Traceback (most recent call last):
File "<pyshell#73>", line 1, in <module>
by + s
TypeError: can't concat bytes to str
>>> s.count(by)
Traceback (most recent call last):
File "<pyshell#74>", line 1, in <module>
s.count(by)
TypeError: Can't convert 'bytes' object to str implicitly
>>> s.count(by.decode("ascii"))
1
>>>
>>> a_string = "深入python"
>>> a_string
'深入python'
>>> len(a_string)
8
>>> by = a_string.encode("uft-8")
Traceback (most recent call last):
File "<pyshell#79>", line 1, in <module>
by = a_string.encode("uft-8")
LookupError: unknown encoding: uft-8
>>> by = a_string.encode("utf-8")
>>> by
b'\\xe6\\xb7\\xb1\\xe5\\x85\\xa5python'
>>> len(by)
12
>>> by = a_string.encode("gb18030")
>>> by
b'\\xc9\\xee\\xc8\\xebpython'
>>> len(by)
10
>>> by = a_string.encode("big5")
>>> by
b'\\xb2`\\xa4Jpython'
>>> len(by)
10
>>> roundtrip = by.decode("big5")
>>> roundtrip
'深入python'
>>> a_string
'深入python'
>>> a_string = roundtrip
>>> a_string == roundtrip
True
>>>
以上是关于Python基础--Pickle/函数默认参数/函数的参数*args/Bytes<=;str/32-64bit/bytes对象的主要内容,如果未能解决你的问题,请参考以下文章
python 使用pickle缓存函数调用(函数名,位置参数,关键字参数)。开销可能很大,所以不要试图使用t