要保证在代码中使用的所有记事本变量都是一致的,可以使用以下方法来避免不一致的记事本变量:
例子:
global x
x = 42
def func1():
print(x)
def func2():
global x
x = 23
func1() # 输出: 42
func2()
func1() # 输出: 23
例子:
x = 42
def func(x):
print(x)
func(x) # 输出: 42
例子:
class MyClass:
x = 42
def func():
print(MyClass.x)
func() # 输出: 42
通过这些方法,可以避免使用不一致的记事本变量而导致代码错误。
上一篇:不一致的精度打印numpy数组
下一篇:不一致的JSON