Python:在构造默认值时使用姊妹参数

标签 python python-2.7

我知道我可以在 scala 中做到这一点,但是有没有办法在 python 中做类似的事情:

def validate_config(data_dir, config_file_dir = data_dir + '/config', json_schema = None):
    print(data_dir)
    print(config_file_dir)
    print(json_schema)

validate_config('data')

我期望的输出是:

data
data/config
None

谢谢!

最佳答案

这不是直接支持的。我会像 Python 避免可变默认实例一样这样做:

def validate_config(data_dir, config_file_dir=None, json_schema=None):
    if config_file_dir is None:
        config_file_dir = data_dir + '/config'
    # rest of code here

关于Python:在构造默认值时使用姊妹参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44268040/

相关文章:

python - 将 numpy 数组拆分为不重叠的数组

python - sklearn.metrics. precision_recall_fscore_support输出的解释

python - 使用 re (雷鬼) 在 python 中查找模式

python - 当函数存储为字典值时调用函数

google-app-engine - 无法在 Google 应用引擎启动器中运行 helloworld

c - 是否可以使用两种编码语言(python 和 C 语言)来制作一个程序?

python - 通过仅提供月份或仅提供星期几来创建日期时间对象

python - 将图像 opencv c++ 发送到 python

python-2.7 - 没有名为 urllib3 的模块 - 尝试安装 pip

python - 为大对象分配名称似乎会大大增加内存使用量