Skip to Content
Python语言及其应用(第2版)
book

Python语言及其应用(第2版)

by Bill Lubanovic
March 2022
Intermediate to advanced
522 pages
13h 52m
Chinese
Posts & Telecom Press
Content preview from Python语言及其应用(第2版)
128
9
>>> def print_data(data, *, start=0, end=100):
... for value in (data[start:end]):
... print(value)
...
>>> data = ['a', 'b', 'c', 'd', 'e', 'f']
>>> print_data(data)
a
b
c
d
e
f
>>> print_data(data, start=4)
e
f
>>> print_data(data, end=2)
a
b
9.3.8
可变实参和不可变实参
还记得吗,如果将同一个列表赋给两个变量,就可以通过任一变量修改此列表。如果两个
变量引用的是整数或字符串,就不会出现这种情况。原因在于列表是可变的,而整数和字
符串是不可变的。
在向函数传递参数时,要小心同样的行为。如果实参是可变的,那么它的值可以通过响应
的形参在函数内部被改变:
6
>>> outside = ['one', 'fine', 'day']
>>> def mangle(arg):
... arg[1] = 'terrible!'
...
>>> outside
['one', 'fine', 'day']
>>> mangle(outside)
>>> outside
['one', 'terrible!', 'day']
最好的做法就是别这么做。
7
要么注明实参可能会被修改,要么使用
return
返回新值。
9.4
文档字符串
易读至上(
readability ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python编程入门与实战

Python编程入门与实战

Posts & Telecom Press, Fabrizio Romano
Python实用技能学习指南

Python实用技能学习指南

Posts & Telecom Press, Robert Smallshire, Austin Bingham
Python技术基础视频教程

Python技术基础视频教程

保罗·J·戴特尔
Python面向对象编程指南

Python面向对象编程指南

Posts & Telecom Press, Steven F. Lott

Publisher Resources

ISBN: 9787115586223