__init__ 函数中声明的实例变量和全局变量的区别
difference between instance variables and global variables declared in __init__ function
class Car(object):
def __init__(self):
self.color = 'red' #var1
global color
color= 'red' #var2
第一个变量和第二个变量有什么区别?
考虑这段代码:
color = 'blue'
class Car(object):
def __init__(self):
self.color = 'green'
global color
color= 'red'
print(color) # prints blue
car = Car()
print(car.color) # prints green
print(color) # prints red
class Car(object):
def __init__(self):
self.color = 'red' #var1
global color
color= 'red' #var2
第一个变量和第二个变量有什么区别?
考虑这段代码:
color = 'blue'
class Car(object):
def __init__(self):
self.color = 'green'
global color
color= 'red'
print(color) # prints blue
car = Car()
print(car.color) # prints green
print(color) # prints red