类型错误:调试()失踪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'**
请建议我在这里做错了什么,我是新来的编码。
答
我不确定你在做什么,但第一个问题是你需要使用提供的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()
的可能的复制[类型错误:缺少1个所需的位置参数: '自我'(http://stackoverflow.com/questions/17534345/typeerror-missing- 1-需要-位置参数的自身) – Somar