quantifiedcode / python-anti-patterns

An open collection of Python anti-patterns and worst practices.
https://quantifiedcode.github.io/python-anti-patterns
Other
1.71k stars 249 forks source link

Question Regarding Global Statements #154

Open Keydex opened 3 years ago

Keydex commented 3 years ago

Hello!

I wanted to read more on the references on this page https://docs.quantifiedcode.com/python-anti-patterns/maintainability/using_the_global_statement.html

I know the book entails things you should "NEVER" do in practice. So I ask this in the context of putting things in the global scope.

Do we consider instantiating built in modules like logger anti patterns? Or should this be something that should be encapsulated in its own class.

import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

Or perhaps this was meant to deter a developer from using the global statement.

The logging function is something I want to instantiate on any files that logs and it seems safe if I define it everywhere. The logging module is intended to be thread-safe without any special work needing to be done by its clients. It achieves this though using threading locks; there is one lock to serialize access to the module’s shared data, and each handler also creates a lock to serialize access to its underlying I/O. https://docs.python.org/3/library/logging.html#thread-safety