name = "james"
class Hello:
global name
def __init__(self,namek):
name = namek
print name
def sayhi(self):
print 'Hello my name is', name
a = Hello('john')
a.sayhi()
这里为什么在调用 a.sayhi() 时全局变量名称没有更改为 john,如下面的代码中 x 的值更改为 2
x = 50
def func():
global x
print('x is', x)
x = 2
print('Changed global x to', x)
func()
print('Value of x is', x)