标签:垃圾回收 ret yield object raise 命名 try geo efi
原则:先定义,后调用
>>> name = ‘李四‘ # 定义
# PEP8规范:等号两边要有空格
>>> print(name) # 引用
李四 # 输出结果
>>>
>>> age = 18
>>> print(age)
18
>>>
>>> x = 10 # x指向10的内存地址 10的引用计数为1
>>> y = x # 10的引用计数为2
>>> z = x # 10的引用计数为3
>>> print(x,y,z)
10 10 10
>>>
>>> x = 10 # x指向10的内存地址
>>> y = x
>>> z = x
>>> print(x,y,z)
10 10 10
>>> del x # 解除变量名x与值10的绑定关系,10的引用计数变为2
>>> print(y)
10
>>> print(z)
10
>>> print(x)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name ‘x‘ is not defined
>>> # 10的引用计数变为2
>>> del y
>>> print(y)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name ‘y‘ is not defined
>>> # 10的引用计数变为1
>>> z = 12345 # 后面定义的变量会把前面定义的覆盖
>>> # 10的引用计数变为0
>>> print(z)
12345
>>> # 10的引用计数变为0
# 见名知意
>>> age = 18
>>> name = ‘alxe‘
# 不能以数字开头
>>> 1 = ‘alex‘
File "<stdin>", line 1
SyntaxError: cannot assign to literal
>>>
# 不能用pytho内置的关键字来命名
>>> print = 666
>>> print(print)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ‘int‘ object is not callable
>>>
# 不要用中文,它不报错
>>> 名字 = ‘李四‘
>>> print(名字)
李四
>>>
# 纯小写加下划线的命名方式
>>> age_of_alex = 23
>>> print(age_of_alex)
23
>>>
# 驼峰体的命名方法
>>> AgeOfAlex = 23
>>> print(AgeOfAlex)
23
>>>
# 查看id
>>> name = ‘alex‘
>>> print(id(name))
2844864947632
# 查看类型
>>> print(type(name))
<class ‘str‘>
# 查看值
>>> print(name)
alex
>>>
>>> name1 = ‘张三‘
>>> name2 = ‘张三‘
>>> print(name1, name2)
张三 张三
>>> print(id(name1), id(name2))
2844864881744 2844864881840
>>># 值相等的情况下,id可能不同,即两个完全不同的内存空间里可以存放两个完全相同的值
# id相同的情况下,值一定相同
>>>
>>> print(name1 is name2)
False
>>> print(name1 == name2)
True
>>>
# int
>>> m = 10
>>> n = 10
>>>
>>> id(m)
140725410912192
>>>
>>> id(n)
140725410912192
>>>
>>> res = 4 + 6
>>> res
10
>>>
>>> id(res)
140725410912192
>>>
# 字符串
>>> x = ‘aaa‘
>>> y = ‘aaa‘
>>>
>>> id(x)
2844864948784
>>> id(y)
2844864948784
>>>
>>> x is y
True
>>>
不变的量
>>> AGE_OF_ALEX = 23
>>> AGE_OF_ALEX = 18
>>> print(AGE_OF_ALEX)
18
>>>
标签:垃圾回收 ret yield object raise 命名 try geo efi
原文地址:https://www.cnblogs.com/linxi321/p/14751912.html