【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
>>> 

不能混用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 "<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
>>> 

















  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值