Why does the same operator print two different values?

As I am binding to understand the concept of self-python I came across this example which I thought was helpful. But there is one part that confuses me. Why print a.i

does it display two different values? In the first case, the output 5

makes sense to me. But then a few lines later, the same statement print a.i

outputs 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()

      

Output

>>> 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

      

+3


source to share


3 answers


You are using global, local and instance attributes with the same name:

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

      



So the output is correct:

>>> 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

      

+5


source


Before the next statement in the method prepare

:

self.i = 123

      



self.i

refers to a class attribute MyClass.i

(since no instance attribute has been set)

After following the instructions, self.i = ..

self.i

enter the new value 123

. (This does not affect the class attribute MyClass.i

, creating a new instance attribute)

+4


source


You change the class variable i to 123 in the function prepare()

:

    self.i = 123

      

After that you call the class variable by executing print a.i

which will print 123 as a result.

+2


source







All Articles