淑昊柠 发表于 2017-5-7 08:42:22

python类的作用域的问题--有趣的现象

这是limodou的ulipad的Debug.py模块,独立运行此模块,会出现异常。
试图改正它后,又有一个错误。
类有属性和方法作用域的问题,还有初始化先后作用域的问题。一般我们很少碰到这样的问题。
声明一下:模块中出现的问题无损于程序的优秀性。这也是我要全文张贴的原因
#   Programmer: limodou
#   E-mail:   limodou@gmail.com
#
#   Copyleft 2006 limodou
#
#   Distributed under the terms of the GPL (GNU Public License)
#
#   UliPad is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
#
#   $Id: Debug.py 1504 2006-09-02 04:08:43Z limodou $
DEBUG = True
import types
import sys
import traceback
import time
import os.path
class Debug:
    def __init__(self, filename=os.path.abspath('debug.txt'), debug=None):
      self.filename = filename
      self.reset(filename)
      self.debug = debug
    def log(self, *args):
      self.output(*args)
    def info(self, *args):
      self.output('[ INFO] -- ', *args)
    def warn(self, *args):
      self.output('[ WARN] -- ', *args)
    def error(self, *args):
      self.output(' -- ', *args)
    def debug(self, *args):
      self.output(' -- ', *args)
    def traceback(self):
      message = traceback.format_exception(*sys.exc_info())
      self.output('', ''.join(message))
    def time(self, *args):
      self.output('[%s] -- ' % time.ctime(time.time()), *args)
    def output(self, *args):
      if self.is_debug():
            #encoding = locale.getdefaultlocale()
            encoding = 'utf-8'
            out = open(self.filename, 'a')
            for i in args:
                if not type(i) in :
                  s= repr(i)
                else:
                  s = i
                if type(s) == type(u''):
                  out.write(s.encode(encoding))
                else:
                  out.write(s)
            out.write("\n")
            out.close()
    def reset(self, filename):
      if self.is_debug():
            open(filename, 'w')
            self.filename = filename
    def is_debug(self):
      if not DEBUG:
            return False
      if self.debug is None:
            return DEBUG
      else:
            return self.debug
debug = None
error = None
if __name__ == '__main__':
    debug = Debug()
    error = Debug(os.path.abspath('error.txt'))
    debug.log('log')
    debug.info('info')
    debug.warn('warn')
    debug.error('error')
    debug.debug('debug')            
 
 
 http://blog.donews.com/ygao/aggbug/1048251.aspx
页: [1]
查看完整版本: python类的作用域的问题--有趣的现象