python中怎样传递参数值
技术百科
冷漠man
发布时间:2025-12-23
浏览: 次 Python参数传递本质是对象引用传递:不可变对象(如数字、字符串)在函数内修改会绑定新对象,原变量不变;可变对象(如列表、字典)调用就地方法会改变原对象;可通过浅拷贝、深拷贝或返回新对象避免意外修改。
Python 中传递参数值主要靠“对象引用传递”,但实际效果常被理解为“按对象传值”——关键看对象是否可变。
不可变对象:看起来像“传值”
数字、字符串、元组等不可变对象,函数内修改会创建新对象,原变量不受影响:
-
例子:
def add_one(x): x += 1; print(x);调用num = 5; add_one(num)输出 6,但num仍是 5 - 本质是
x += 1重新绑定了局部变量x到新整数对象,没改原对象
可变对象:修改会影响原对象
列表、字典、集合等可变对象,函数内直接调用方法(如 .append()、.update())会改变原对象:
-
例子:
def append_item(lst): ls;调用
t.append('new')my_list = [1]; append_item(my_list)后my_list变成[1, 'new']
- 因为
lst 和my_list指向同一个列表对象,操作的是它本身
想避免意外修改?主动复制
若不希望函数改动原始可变对象,传入前手动拷贝:
- 浅拷贝:
func(my_list.copy())或func(my_list[:])(仅适用于列表) - 深拷贝(嵌套结构):
import copy; func(copy.deepcopy(my_dict)) - 函数内部也可先复制:
local_lst = lst.copy(),再操作local_lst
显式控制:用返回值代替就地修改
更清晰、更安全的做法是让函数返回新对象,由调用方决定是否赋值:
-
推荐写法:
def add_item(lst, item): return lst + [item];然后new_list = add_item(old_list, 'x') - 这样逻辑明确,原列表不变,也方便链式调用或测试
基本上就这些。记住核心:Python 传的是对象的引用,但“能不能改原对象”,取决于对象类型和你用了什么操作。
相关栏目:
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
AI推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
SEO优化<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
技术百科<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
谷歌推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
百度推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
网络营销<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
案例网站<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
精选文章<?muma echo $count; ?>
】
相关推荐
- php485能和物联网模块通信吗_php485对接
- 电脑的“网络和共享中心”去哪了_Windows 1
- 如何在Golang中操作嵌套切片指针_Golang
- Win11怎么查看电脑配置_Win11硬件配置详细
- 如何使用Golang模拟请求超时_Golang c
- Mac怎么设置登录项_Mac管理开机自启动程序【教
- C++如何使用std::optional?(处理可
- Win11怎么设置闹钟_Windows 11时钟应
- Python解释执行模型_字节码流程说明【指导】
- Windows10电脑怎么连接蓝牙设备_Win10
- Python网络超时处理_健壮性设计说明【指导】
- php打包exe如何加密代码_防反编译保护方法【技
- Win11笔记本怎么看电池健康度_Win11电池报
- Windows 11无法安全删除U盘提示设备正在使
- php485函数执行慢怎么优化_php485性能提
- Windows蓝屏错误0x00000018怎么处理
- 如何在 Go 后端安全获取并验证前端存储的 JWT
- 如何使用 Selenium 正确获取篮球参考网站球
- 如何使用Golang benchmark测量函数延
- Win11怎么设置任务栏对齐方式_Windows1
- Win11怎样激活系统密钥_Win11系统密钥激活
- 手机php文件怎么变成mp4_安卓苹果打开php转
- Win10怎么查看内存时序参数_Win10CPU-
- Win11如何设置文件权限 Win11 NTFS文
- Win11怎么设置组合键快捷方式_Windows1
- MAC怎么使用表情符号面板_MAC Emoji快捷
- 如何在Golang中使用container/hea
- c# 在高并发下使用反射发射(Reflection
- c++ nullptr与NULL区别_c++11空
- 如何在 Go 中正确测试带 Cookie 的 HT
- Windows11如何设置专注助手_Windows
- Python对象生命周期管理_创建销毁解析【教程】
- Windows10怎么备份注册表_Windows1
- Win11怎么调整屏幕亮度_Windows 11调
- MAC如何安装Git版本控制工具_MAC开发环境配
- PowerShell怎么创建复杂的XML结构
- 如何在JavaScript中动态拼接PHP的bas
- c++ atoi和atof函数用法_c++字符数组
- 当网站SEO排名下降时,如何应对?
- 如何使用正则表达式精确匹配最多含一个换行符的 st
- php485函数怎么捕获异常_php485错误处理
- php嵌入式日志记录怎么实现_php将硬件数据写入
- Win11怎么开启空间音效_Windows11耳机
- 企业SEO优化选择网站建设模板的技巧
- Win11怎么查看wifi信号强度_检测Windo
- 如何在Golang中理解指针比较_Golang地址
- Win11怎么关闭小组件_Win11禁用任务栏天气
- Windows11怎么自定义任务栏_Windows
- Win10如何备份驱动程序_Win10驱动备份步骤
- Win11怎么查看显卡显存_查询Win11显卡详细

t.append('new')
QQ客服