Why does python modify the global int variable in the method of the class without reporting an error but invalid after adding global?

I am trying to write a command line Mini Game with python, in which there is a class that needs to modify a global int variable. I obviously have added global ( Baidu result , etc.), and the code has not reported an error, but why has it not been modified when accessing the variable again? Solution ~
Environment: mac+python3.6+pycharm+ipython
my Code:

-sharp python
obj = {"s": []}
sunlight = 0


class GameObject:
    indicating_char = ""

    def __init__(self, pos):
        self.pos = pos
        obj[self.indicating_char].append(self)

    def __str__(self):
        return self.indicating_char

    def step(self): pass


class Sunflower(GameObject):
    def __init__(self, pos):
        self.indicating_char = "s"
        super().__init__(pos)

    def step(self):
        print("executing")
        global sunlight
        sunlight += 50

-sharp ipython
In [1]: from game import *

In [2]: Sunflower(0).step()
executing

In [3]: sunlight
Out[3]: 0
Mar.21,2021

use it this way:

import game


game.SunFlower(0).step()
print(game.sunlight)

declare the global variable at the beginning:

obj = {'s': []}
global sunlight
sunlight = 0

Menu