-
函数定义时接收不定长参数
def foo(*args, **kwargs):
pass
-
参数传递
def foo(x, y, z, a, b):
print(x)
print(y)
print(z)
print(a)
print(b)
lst = [1, 2, 3]
dic = {'a': 22, 'b': 77}
foo(*lst, **dic)
-
import * 语法
-
文件 xyz.py
__all__ = ('a', 'e', '_d')
a = 123
_b = 456
c = 'asdfghjkl'
_d = [1,2,3,4,5,6]
e = (9,8,7,6,5,4)
-
文件 abc.py
from xyz import *
print(a)
print(_b)
print(c)
print(_d)
print(e)
-
2. Python 的赋值和引用
-
==, is
:==
判断的是值,is
判断的是内存地址 (即对象的id) -
小整数对象: [-5, 256]
-
copy, deepcopy
的区别-
copy
: 只拷贝表层元素 -
deepcopy
: 在内存中重新创建所有子元素
-
-
练习1: 说出执行结果
def extendList(val, lst=[]):
lst.append(val)
return lst
list1 = extendList(10)
list2 = extendList(123, [])
list3 = extendList('a')
-
练习2: 说出下面执行结果
from copy import copy, deepcopy
from pickle import dumps, loads
a = ['x', 'y', 'z']
b = [a] * 3
c = copy(b)
d = deepcopy(b)
e = loads(dumps(b, 4))
b[1].append(999)
b.append(777)
c[1].append(999)
c.append(555)
d[1].append(999)
d.append(333)
e[1].append(999)
e.append(111)
-
自定义 deepcopy: