Int-0
Int-0

Reputation: 109

Why does this python code work?

I have written a simple python module, it has this code:

_log = logging.getLogger("mymodule")
_started = False

def set_log_level(level):
    _log.setLevel(level)
    if not _started:
        _hdlr = logging.FileHandler('mymodule.log')

When I call set_log_level() program fails because symbol _started is not found. It is normal because global _started is missing in the method. But my question is: symbol _log has the same visibility as _started, so why does this symbol can be found?

Upvotes: 1

Views: 170

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599846

I don't think your program fails for the reason you think. There's no need to use the global declaration within a function unless you actually modify that variable. (Otherwise, you'd need to use global for every access of something in that namespace, even calling other functions.)

I'm not sure what actually happens when you run your program - perhaps you could provide a traceback or description of the problem.

Upvotes: 4

Related Questions