2017-03-04 41 views
-1
1. first.py file 

import logging 
class Log: 
    def __init__(self,msg): 
     self.msg = msg 

    def debug(self,msg): 
     FORMAT = "%(asctime)-15s%(message)s" 
     logging.basicConfig(filemode = "w", filename="file.log", format = FORMAT, level=logging.DEBUG) 
     logging.debug(self.msg) 

2. second.py file 

import first 
first.Log.debug("I am in debug mode") 

**调用方法,当我运行second.py文件比我得到错误的 Logging.Log.debug(“我什么时候我在调试模式“)类型错误:调试()失踪1个人需要的位置参数:“味精”从不同类

TypeError: debug() missing 1 required positional argument: 'msg'** 

请建议我在这里做错了什么,我是新来的编码。

+1

的可能的复制[类型错误:缺少1个所需的位置参数: '自我'(http://stackoverflow.com/questions/17534345/typeerror-missing- 1-需要-位置参数的自身) – Somar

回答

0

我不确定你在做什么,但第一个问题是你需要使用提供的msg参数来初始化Log的实例。你在这里做什么first.Log.debug("I am in debug mode")正在调用debug方法Log而不创建一个实例。

在您的debug方法msg参数是必需的,但它从来没有使用过。相反,该方法将仅尝试获取self.msg定义的__init__

其中一种方法的代码将工作:

1. first.py file 

import logging 
class Log: 
    def __init__(self,msg): 
     self.msg = msg 

    def debug(self): 
     FORMAT = "%(asctime)-15s%(message)s" 
     logging.basicConfig(filemode = "w", filename="file.log", format = FORMAT, level=logging.DEBUG) 
     logging.debug(self.msg) 

2. second.py file 

import first 
# Note that Log is initialized with the msg you want, and only then we call debug() 
first.Log("I am in debug mode").debug() 
相关问题