29030

One class instance used automatically throughout process

Question:

The python logging module once imported and instantiated will be so across the process, inside all modules and threads. How did they achieve that effect?

<strong>Example:</strong>

<strong>myapp.py</strong>

import logging import mylib def main(): logging.basicConfig(filename='myapp.log', level=logging.INFO) logging.info('Started') mylib.do_something() logging.info('Finished') if __name__ == '__main__': main()

<strong>mylib.py</strong>

import logging def do_something(): logging.info('Doing something')

<strong>myapp.log</strong>

INFO:root:Started INFO:root:Doing something INFO:root:Finished

Answer1:

When a module is imported for the first time, the loaded module object is put into sys.modules. Later imports will then find the module object and not reload the module.

The logging module has a bunch of module attributes which hold the state of logging configuration after the first import.

Recommend

  • Restarting a thread in Python
  • Get default sql value from Entity Framework class
  • Ruby date format validation
  • How to fix AttributeError: 'module' object has no attribute 'Client' when runnin
  • Logging from a child module ignores the added root logger stream handler
  • How to Map() a function recursively (through nested lists) in R?
  • How to Export CSV file from ASP.NET core
  • How to populate a Mutation for a different Cassandra table in a trigger
  • How to get a Builder object from rows related to pivot - Laravel
  • How to upload an Image onto localhost server whilst path is stored in mysql database?
  • Using Facebook Graph API with ASP.NET
  • Returning local reference created by JNI from a native method
  • no endpoints available for service \\“kubernetes-dashboard\\”
  • How can I find the difference between two times [duplicate]
  • C++ How to get process ID from filename of DLL(windows)?
  • Enable CORS on Tomcat 8.0.30
  • JavaFX Embed Custom Font Not Working
  • Send HTML Mail with Unicode
  • Shrinking Bootstrap Navbar with logo on scroll
  • Jenkins sending notifications to the wrong commit id
  • Could not resolve all files for configuration ':react-native-vector-icons:classpath'
  • How can i use JQuery fadeTo() in IE 7?
  • How to move to lines with the same indentation in Visual Studio Code
  • Allowing audio files in Spring MVC 3.0?
  • Windows biometric framework sample umdf driver: This device cannot start. (Code 10)
  • Magento-Change Attribute of All Products
  • Generate and export point cloud from Project Tango
  • Multiplying polynomials/simplifying like terms
  • Comparing variables with strings bash
  • JavaScript Regex to Match Boundaries of Words with diacritics
  • How to turn off notice reporting in xampp?
  • Codeigniniter insert data through models and controller
  • Sign a Pdf using custom digital signature in Java
  • Jersey serializes character value to ASCII equivalent numeric string
  • Angular FormGroup won't update it's value immediately after patchValue or setValue