1、什么是pickling和unpickling?
pickling是将Python对象(甚至是Python代码),转换为字符串的过程。
unpickling是将字符串,转换为原来对象的逆过程。
2、解释*args和**kwargs?
*args,是当我们不确定要传递给函数参数的数量时使用的。
def add(* num):
sum = 0
for val in num:
sum = val + sum
print(sum)
add(4,5)
add(7,4,6)
add(10,34,23)
---------------------
9
17
67
**kwargs,是当我们想将字典作为参数传递给函数时使用的。
def intro(**data):
print("\nData type of argument:",type(data))
for key, value in data.items():
print("{} is {}".format(key,value))
intro(name="alex",Age=22, Phone=1234567890)
intro(name="louis",Email="a@gmail.com",Country="Wakanda", Age=25)
--------------------------------------------------------------
Data type of argument: <class 'dict'>
name is alex
Age is 22
Phone is 1234567890
Data type of argument: <class 'dict'>
name is louis
Email is a@gmail.com
Country is Wakanda
Age is 25
3、解释re模块的split()、sub()、subn()方法?
split():只要模式匹配,此方法就会拆分字符串。
sub():此方法用于将字符串中的某些模式替换为其他字符串或序列。
subn():和sub()很相似,不同之处在于它返回一个元组,将总替换计数和新字符串作为输出。
import re
string = "There are two ball in the basket 101"
re.split("\W+",string)
---------------------------------------
['There', 'are', 'two', 'ball', 'in', 'the', 'basket', '101']