属性和普通变量使用增强赋值语句时的差异
来源:互联网 发布:js 按键监听 编辑:程序博客网 时间:2024/05/22 02:09
1.API中相关描述
2.示例
1
num = 4class Test: num = 3 def f(self): self.num += 2 print(self.num) # 5 num += 3 # UnboundLocalError: local variable 'num' referenced before assignment print(num)x = Test()x.f()'''为什么self.num += 2正常执行,num += 3报错?这里为方便,忽略增强赋值语句和普通赋值语句的差异,从普通赋值语句分析既问题变为:为什么self.num = self.num + 2正常执行而num = num + 3报错?1.对于类对象属性或实例对象的数据属性 1.右边的num可能是类属性或实例对象的数据属性 2.左边的num是实例的数据属性,在必要时会自动创建2.对于普通变量 左右两边的num最终均会被解析为局部变量'''
再来点补充知识:
9.2. Python Scopes and Namespaces
A special quirk of Python is that – if noglobal
statement is in effect – assignments to names always go into the innermost scope. Assignments do not copy data — they just bind names to objects. The same is true for deletions: the statement del x
removes the binding of x
from the namespace referenced by the local scope. In fact, all operations that introduce new names use the local scope: in particular, import
statements and function definitions bind the module or function name in the local scope. 0 0
- 属性和普通变量使用增强赋值语句时的差异
- python中赋值语句和增强赋值语句的区别
- 常量、变量、运算符、表达式和赋值语句的使用
- 静态加载顺序,类static变量也有默认值(不同于C++),普通成员变量直接赋值语句和非静态语句块的执行位置
- 属性和普通变量区别
- java增强for循环与普通for的差异
- 赋值和属性变量下划线
- 联合变量的赋值和使用
- 联合变量的赋值和使用
- enum类型变量的使用和赋值
- SQL之变量的使用和赋值
- 关于使用select语句对变量赋值的注意事项
- 黑马程序员 WPF中DataGrid的ItemsSource属性的赋值和取值的差异
- 原始数据类型和对象类型赋值时的差异
- 普通赋值和引用赋值
- 对象的赋值和普通赋值的区别
- string和int转换,增强赋值语句,list的append和extend,字典的初始化和update,冒泡排序
- 关于php的普通赋值和引用赋值
- Spring MVC拦截器DispatcherServlet详解
- HTTP Status 500
- unity中BringWindowToFront和dragWindow的用法
- sql 语句as用法
- LeetCode 208 Implement Trie(实现字典树)
- 属性和普通变量使用增强赋值语句时的差异
- matplotlib的基本用法(四)——设置legend图例
- Dagger 2从浅到深(一)
- Oracle那些进程帮我们做些什么
- 线程、线程池总结
- PonyCar体验感受
- javascript的内置对象string对象总结
- 简单配置spring+springmvc+mybatis之整合
- 第三讲、Linux常用命令