【Python基础】--Pickle/函数默认参数/函数的参数*args/Bytesstr/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']]
>>> 

【Python基础】--Pickle/函数默认参数/函数的参数*args/Bytesstr/32-64bit/bytes对象_第1张图片

函数默认参数

>>> 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
>>> 
【Python基础】--Pickle/函数默认参数/函数的参数*args/Bytesstr/32-64bit/bytes对象_第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)
【Python基础】--Pickle/函数默认参数/函数的参数*args/Bytesstr/32-64bit/bytes对象_第3张图片

Bytes<=>str

str对象和bytes对象可以使用.encode() (str -> bytes) or .decode() (bytes -> str)方法相互转化。

>>> b = b'china'
>>> b
b'china'
>>> type(b)

>>> 
>>> s = b.decode()
>>> s
'china'
>>> bl = s.encode()
>>> bl
b'china'
>>> 

【Python基础】--Pickle/函数默认参数/函数的参数*args/Bytesstr/32-64bit/bytes对象_第4张图片

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')
>>> 

【Python基础】--Pickle/函数默认参数/函数的参数*args/Bytesstr/32-64bit/bytes对象_第5张图片

>>> by = b"abcde"
>>> len(by)
5
>>> by += b"f"
>>> by
b'abcdef'
>>> by[0]
97
>>> by[0] = 98
Traceback (most recent call last):
  File "", line 1, in 
    by[0] = 98
TypeError: 'bytes' object does not support item assignment
>>> 

不能混用bytesstrings

不能连接bytes对象和字符串。他们两种不同的数据类型。

也不允许针对字符串中bytes对象的出现次数进行计数,因为串里面根本没有bytes。字符串是一系列的字符序列。也许你是想要先把这些字节序列通过某种编码方式进行解码获得字符串,需要显式地指明它。Python 3不会隐含地将bytes转换成字符串,或者进行相反的操作。

>>> by = b"d"
>>> s = "abcde"
>>> by
b'd'
>>> s
'abcde'
>>> by + s
Traceback (most recent call last):
  File "", line 1, in 
    by + s
TypeError: can't concat bytes to str
>>> s.count(by)
Traceback (most recent call last):
  File "", line 1, in 
    s.count(by)
TypeError: Can't convert 'bytes' object to str implicitly
>>> s.count(by.decode("ascii"))
1
>>> 
【Python基础】--Pickle/函数默认参数/函数的参数*args/Bytesstr/32-64bit/bytes对象_第6张图片

>>> a_string = "深入python"
>>> a_string
'深入python'
>>> len(a_string)
8
>>> by = a_string.encode("uft-8")
Traceback (most recent call last):
  File "", line 1, in 
    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/Bytesstr/32-64bit/bytes对象_第7张图片

















你可能感兴趣的:(【Python】)