python variable sharing between packages/modules

前端 未结 3 1729
离开以前
离开以前 2021-01-23 16:02

trying to understand and learn how to write packages... testing with something i\'ve always used, logging...

Can you please help me understand why the \'log\' variable i

相关标签:
3条回答
  • 2021-01-23 16:49

    log is a global variable in the differentlogging module. Thus you can access it as clusterlogging.differentlogging.log.

    You could also do something like from clusterlogging.differentlogging import log and then access it as just log.

    Edit: actually, on reviewing your code again I don't know what to make of it. Could you please fix up your code indentation so that it makes sense? Are you defining log inside the consolelogging function? If so, you'll need to either make it global with global log or return it from the function and assign it to a variable log on the line where you call the function.

    0 讨论(0)
  • 2021-01-23 16:50

    Your main.py doesn't do anything to define the name log in the global namespace. Importing a module can define names in the namespace of that module, but can't put anything in the global namespace.

    In your main.py you should add this statement:

    from clusterlogging.differentlogging import log
    

    By the way, I that is such a long module name, I would use import as:

    import clusterlogging.differentlogging as difflogging
    log = difflogging.log
    

    EDIT: I originally recommended this but it won't work:

    from difflogging import log  # doesn't work
    

    You might even want to use a really short name like dl:

    import clusterlogging.differentlogging as dl
    dl.log('whatever')
    

    Since dl is really short, maybe you don't need to get log bound in the global namespace.

    Also, you could get every name from a module by using import * but this is not recommended.

    from clusterlogging.differentlogging import *  # not recommended
    

    You usually don't want to clutter the global namespace with all the stuff defined in a module. Import just what you need. This is tidier and helps document what you are actually using.

    0 讨论(0)
  • 2021-01-23 16:57

    This will return the log array, and you will be able to use the logging function associated.

    main.py:

    #!/usr/bin/env python
    
    import sys
    
    sys.path.append('CLUSTER')
    import clusterlogging.differentlogging
    log=clusterlogging.differentlogging.ttylogging()
    
    log.debug("Logging module loaded")
    log.info ("It worked")
    

    differentlogging.py :

    #!/usr/bin/env python
    
    def ttylogging():
        print "Console Logging loaded"
        import sys
        import logging
        class NullHandler(logging.Handler):
            def emit(self, record):
                pass
    
        DEFAULTLOGLEVEL=logging.INFO
        log = logging.getLogger(__name__)
        log.addHandler(NullHandler())
    
        log.setLevel(DEFAULTLOGLEVEL)
        logStreamHandler = logging.StreamHandler(sys.stdout)
        logStreamHandler.setFormatter(logging.Formatter("%(asctime)s %(levelname)5s %(name)s %(lineno)d: %(message)s"))
        log.addHandler(logStreamHandler)
    
        return log  
    
    def mysqllogging():
        print "mysql logging module here"
    
    def sysloglogging():
        print "rsyslog logging module here"
    
    0 讨论(0)
提交回复
热议问题