2017-03-04 40 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() 
相關問題