-1

I know this is a stupid question, but it's happening on my side.

def func():
    print(name)


if __name__ == "__main__":
    name = "halo"
    print(name)

    func()

As I know, func( ) can't see name variable. But its showing "halo" twice.

I am working in PyCharm. Any suggestion? Or python originally works in this way?

cogito
  • 29
  • 1
  • 4

1 Answers1

3

func can see the name variable. It is a global variable, because the place where you created it is not inside any class or function. All functions can read global variables.

khelwood
  • 55,782
  • 14
  • 81
  • 108