如何组合已知参数和可变参数以将它们传递给另一个函数?
How do I combine known arguments and variable arguments to pass them to another function?
我在 Python 中看到了以下关于变量参数的 SO 帖子:
What does ** (double star) and * (star) do for parameters?
function call with named/unnamed and variable arguments in python
Can a variable number of arguments be passed to a function?
None 个回答了我的问题。所以,这里是:
我想定义几个函数:
def LogError(log_file, *args):
print("USER ERROR:", ***NOT SURE ABOUT THIS PART***, file=log_file)
def LogWarning(log_file, *args):
print("USER WARNING:", ***NOT SURE ABOUT THIS PART***, file=log_file)
我想给他们打电话:
error_file = open("somefile")
LogError(error_file, "Unable to find", username, "in the database.")
warning_file = open("somefile")
LogWarning(warning_file, arg1, arg2, arg3)
我希望对 LogError
的调用等同于
print("USER ERROR:", "Unable to find", username, "in the database.", file=error_file)
我希望对 LogWarning
的调用等同于
print("USER WARNING:", arg1, arg2, arg3, file=warning_file)
实现该目标的最佳方法是什么?
您的第一个 link 应该提供答案
def LogError(log_file, *args):
print("USER ERROR:", *args, file=log_file)
def LogWarning(log_file, *args):
print("USER WARNING:", *args, file=log_file)
这将根据需要扩展参数。但是与其制定自己的解决方案,不如看看 python's built in logging feature。
只要加入他们 space,因为这就是最终结果:
print('USER ERROR: {}'.format(' '.join(*args)), file=error_file)
你真的应该使用 logging module,这会让你的生活更轻松:
import logging
# Create a logger
log = logging.getLogger(__file__)
# Set the output for different log levels
# this is optional, you can write out all errors to the
# console and do many combinations, such as sending emails
warning_logger = logging.FileHandler('warnings.log')
warning_logger.setLevel(logging.WARNING)
error_logger = logging.FileHandler('error.log')
error_logger.setLevel(logging.ERROR)
# Set the format of messages
# levelname will be WARNING or ERROR depending on the message
log_format = logging.Formatter('USER %(levelname)s: %(message)s')
# Set the formatters for the error loggers
warning_logger.setFormatter(log_format)
error_logger.setFormatter(log_format)
# Add the handlers to the master logger instance
log.addHandler(warning_logger)
log.addHandler(error_logger)
# Now all you need is
log.warn('This is a warning')
log.error('This is a error')
有关详细信息,请查看 logging module documentation and for more recipes, the logging module cookbook 值得一读。
我在 Python 中看到了以下关于变量参数的 SO 帖子:
What does ** (double star) and * (star) do for parameters?
function call with named/unnamed and variable arguments in python
Can a variable number of arguments be passed to a function?
None 个回答了我的问题。所以,这里是:
我想定义几个函数:
def LogError(log_file, *args):
print("USER ERROR:", ***NOT SURE ABOUT THIS PART***, file=log_file)
def LogWarning(log_file, *args):
print("USER WARNING:", ***NOT SURE ABOUT THIS PART***, file=log_file)
我想给他们打电话:
error_file = open("somefile")
LogError(error_file, "Unable to find", username, "in the database.")
warning_file = open("somefile")
LogWarning(warning_file, arg1, arg2, arg3)
我希望对 LogError
的调用等同于
print("USER ERROR:", "Unable to find", username, "in the database.", file=error_file)
我希望对 LogWarning
的调用等同于
print("USER WARNING:", arg1, arg2, arg3, file=warning_file)
实现该目标的最佳方法是什么?
您的第一个 link 应该提供答案
def LogError(log_file, *args):
print("USER ERROR:", *args, file=log_file)
def LogWarning(log_file, *args):
print("USER WARNING:", *args, file=log_file)
这将根据需要扩展参数。但是与其制定自己的解决方案,不如看看 python's built in logging feature。
只要加入他们 space,因为这就是最终结果:
print('USER ERROR: {}'.format(' '.join(*args)), file=error_file)
你真的应该使用 logging module,这会让你的生活更轻松:
import logging
# Create a logger
log = logging.getLogger(__file__)
# Set the output for different log levels
# this is optional, you can write out all errors to the
# console and do many combinations, such as sending emails
warning_logger = logging.FileHandler('warnings.log')
warning_logger.setLevel(logging.WARNING)
error_logger = logging.FileHandler('error.log')
error_logger.setLevel(logging.ERROR)
# Set the format of messages
# levelname will be WARNING or ERROR depending on the message
log_format = logging.Formatter('USER %(levelname)s: %(message)s')
# Set the formatters for the error loggers
warning_logger.setFormatter(log_format)
error_logger.setFormatter(log_format)
# Add the handlers to the master logger instance
log.addHandler(warning_logger)
log.addHandler(error_logger)
# Now all you need is
log.warn('This is a warning')
log.error('This is a error')
有关详细信息,请查看 logging module documentation and for more recipes, the logging module cookbook 值得一读。