【脚本语言系列】关于Python基础知识对象变动,你知道的事
来源:互联网 发布:c语言获取随机数 编辑:程序博客网 时间:2024/06/09 20:37
如何控制对象可变性
- 对象可变性(mutation),可变(mutable)
# -*- coding:utf-8 -*-the_name = ['Allen']print the_nameother_name = the_nameother_name += ['Moore']print other_nameprint the_name
['Allen']['Allen', 'Moore']['Allen', 'Moore']
# -*- coding:utf-8 -*-def append_to(item, target=[]): target.append(item) return targettarget=append_to(12)print targettarget=append_to(23)print targettarget=append_to(34)print target
[12][12, 23][12, 23, 34]
- 不可变(immutable)
# -*- coding:utf-8 -*-def append_to(item, target=None): if target is None: target = [] target.append(item) return targettarget=append_to(12)print targettarget=append_to(23)print targettarget=append_to(34)print target
[12][23][34]
阅读全文
0 0
- 【脚本语言系列】关于Python基础知识对象变动,你知道的事
- 【脚本语言系列】关于Python基础知识面向对象编程,你需要知道的事
- 【脚本语言系列】关于Python基础知识对象自省,你需要知道的事
- 【脚本语言系列】关于Python基础知识处理字符串,你需要知道的事
- 【脚本语言系列】关于Python基础知识文件操作,你需要知道的事
- 【脚本语言系列】关于Python基础知识设计模式,你需要知道的事
- 【脚本语言系列】关于Python基础知识魔法变量,你需要知道的事
- 【脚本语言系列】关于Python基础知识调试,你需要知道的事
- 【脚本语言系列】关于Python基础知识协程,你需要知道的事
- 【脚本语言系列】关于Python基础知识函数缓存,你需要知道的事
- 【脚本语言系列】关于Python基础知识映射器和过滤器,你需要知道的事
- 【脚本语言系列】关于Python基础知识__slots__,你需要知道的事
- 【脚本语言系列】关于Python基础知识Lambda表达式,你需要知道的事
- 【脚本语言系列】关于Python基础知识枚举,你需要知道的事
- 【脚本语言系列】关于Python基础知识异常处理,你需要知道的事
- 【脚本语言系列】关于Python基础知识推导式,你需要知道的事
- 【脚本语言系列】关于Python基础知识容器,你需要知道的事
- 【脚本语言系列】关于Python基础知识多返回值,你需要知道的事
- springmvc的restful化
- zabbix表达式
- css学习笔记之包含块
- lnmp服务器配置
- React-引领未来的用户界面开发框架-读书笔记(六)
- 【脚本语言系列】关于Python基础知识对象变动,你知道的事
- messager(消息窗口) 的使用
- Homework_7
- Android——用户界面设计(布局)
- iOS socket 套接字编程
- cut
- Eclipse中使用相对路径来引用Library
- 64. Minimum Path Sum
- React-引领未来的用户界面开发框架-读书笔记(七)