Python中的变量引用对象需注意的几点 分类:Python (55) (0) 普通引用: Python中,变量的作用仅仅是一个标识,只有赋值后才被创建,它可以引用任何类型的对象,而且在引用之前必须赋值.赋值后的变量指向响应的对象,拥有该对象的空间.类型属于对象,但是不是变量. a = 3 a = "science" 上述代码说明数值3和字符串“science”分别是两种对象,初始变量a赋值对象3被创建,变量a指向对象3的内存空间,之后变量a又指向了字符串的内存空间. 共享引用: a
Python中的任何一条数据都是对象.每个对象都由3部分组成:标识.类型和值.对象的标识代表该对象在内存中的存储位置(因此是不可更改的),对象的类型表明它可以拥有数据和值的类型. 创建 Dog 类根据 Dog 类创建的每个实例都将存储名字和年龄.我们赋予了每条小狗蹲下( sit() )和打滚( roll_over() )的能力: dog.py class Dog(): """一次模拟小狗的简单尝试""" def __init__(self, na