python accessing global variable in the method.? -


kindly bare if question silly, c/c++ background.

i have following code.

#!/usr/bin/python  import os   class logger(object):      def __init__ (self):         print "constructor of logger "      def logmsg(self):         print "logmsg::"   class filelogger (logger):     def __init__ (self):         print "constructor of file logger"     def logmsg (self):         print "filelogger::"  class ftplogger (logger):      def __init__ (self):         print "constructor of ftp logger"      def logmsg (self):         print "ftplogger::"   def logmsg(log):     print "logging message"     loghandler.logmsg()    # **here: how possible access loghandler variable?**   loghandler = filelogger (); logmsg(loghandler); 

question:

how logmsg() function of filelogger class able access loghandler?.

can consider 'loghandler'is global variable?

when define loghandler variable outside function, variable becomes global in scope within module (essentially python file) , can see (and refer it) inside function logmsg(log).

however, in context of code you've written, seems me using loghandler way mistake, since you're passing loghandler object logmsg function. logmsg function should this, think:

def logmsg(log):     print "logging message"     log.logmsg() 

otherwise, when pass different object function, trying access whichever 1 in global variable loghandler , not 1 passed in in log.

the answers other question provide more detail python scoping:

short description of scoping rules?


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -