2017-03-04 1 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") 

**에서 메서드를 호출 할 때 디버그 모드에 있습니다. ")형식 오류 : 디버그 (1 개) 필요한 위치 인수 누락 'MSG를'나는 " Logging.Log.debug (나는 오류를 얻고보다 내가 second.py 파일을 실행하면 다른 클래스

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

여기서 내가 잘못한 것을 제안하십시오. 코딩에 익숙하지 않습니다.

+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 인수가 필요하지만 결코 사용되지 않습니다. 대신이 메서드는 __init__에 정의 된 self.msg을 가져 오는 것입니다.

한 가지 방법이 코드가 작동 것이다 :

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() 
관련 문제