繁体   English   中英

为什么同一语句打印两个不同的值?

[英]Why does the same statement print two different values?

当我想要理解python自我概念时,我遇到了这个我认为有用的例子。 但是有一部分让我感到困惑。 为什么print ai输出两个不同的值? 在第一种情况下,输出为5 ,这对我来说很有意义。 但后来几行同样的print ai语句输出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

您正在使用具有相同名称的全局,本地和实例属性:

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

prepare方法中的以下声明之前:

self.i = 123

self.i引用类属性MyClass.i (因为未设置实例属性)

执行self.i = ..语句后, self.i引用新值123 (这不会影响类属性MyClass.i ,从而产生新的实例属性)

您正在prepare()函数中将类变量i更改为123:

    self.i = 123

之后,您通过执行print ai调用类变量,结果将打印123。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM