Can variables in the local scope get reverted after global function has been declared?

  python, python-3.x
i=7
def myfunc():
    i=2
    global i
    i=i+3
    print(i)
    print(i)
myfunc()

My book gives the output to be:

10
2

It says the variable i is re-assigned back to 2 which does not make sense to me as I learned global can’t be reverted back from a different book.

When running the code I get an error saying "i is defined before global declaration" which I understood by reading previous questions on this site.

So my question would be, why has my book plastered this code and its variations everywhere if it is wrong? Is it from a previous version? I do not have a complete understanding of python and its previous version as I study computer science only to pass high school. And does global get reverted here?

Source: Python Questions

LEAVE A COMMENT