当我想要理解python自我概念时,我遇到了这个我认为有用的例子。但是有一部分让我感到困惑。为什么print a.i
会输出两个不同的值?在第一种情况下,输出为5
,这对我来说很有意义。但是后来几行print a.i
语句输出123
。
def say_hi():
return 'hi!'
i = 789
class MyClass(object):
i = 5
def prepare(self):
i = 10
self.i = 123
print i
def say_hi(self):
return 'Hi there!'
def say_something(self):
print say_hi()
def say_something_else(self):
print self.say_hi()
输出
>>> print say_hi()
hi!
>>> print i
789
>>> a = MyClass()
>>> a.say_something()
hi!
>>> a.say_something_else()
Hi there!
>>> print a.i
5
>>> a.prepare()
10
>>> print i
789
>>> print a.i
123
答案 0 :(得分:5)
您正在使用具有相同名称的全局,本地和实例属性:
def say_hi(): # This is the global function 'say_hi'
return 'hi!'
i = 789 # This is the global 'i'
class MyClass(object):
i = 5 # This is a class attribute 'i'
def prepare(self):
i = 10 # Here, you are creating a new 'i' (local to this function)
self.i = 123 # Here, you are changing the instance attribute 'i'
print i # Here, you are printing the new'ed 'i' (now with value 10)
def say_hi(self): # This is the object method 'say_hi' function
return 'Hi there!'
def say_something(self):
print say_hi() # Here, you are calling the global 'say_hi' function
def say_something_else(self):
print self.say_hi() # Here, you are calling the object method 'say_hi' function
所以输出是正确的:
>>> print say_hi() # global
hi!
>>> print i # global
789
>>> a = MyClass()
>>> a.say_something() # say_something calls the global version
hi!
>>> a.say_something_else() # say_something_else calls the object version
Hi there!
>>> print a.i # class attribute 'i'
5
>>> a.prepare() # prints the local 'i' and change the class attribute 'i'
10
>>> print i # global 'i' is not changed at all
789
>>> print a.i # class attribute 'i' changed to 123 by a.prepare()
123
答案 1 :(得分:4)
在prepare
方法中的以下陈述之前:
self.i = 123
self.i
引用类属性MyClass.i
(因为未设置实例属性)
执行self.i = ..
语句后,self.i
会引用新值123
。 (这不会影响类属性MyClass.i
,从而产生新的实例属性)
答案 2 :(得分:2)
您正在prepare()
函数中将类变量i更改为123:
self.i = 123
之后,您通过print a.i
调用类变量,结果将打印123.