python - 如何在函数参数中留出空格?

标签 python python-3.x python-2.7

当以下行的两个单词之间有空格时,以下代码如何正确工作以使每个参数都等于 true?

custom = detector.CustomObjects(cell phone=True, car=True)

这来自 ImageAI 库,下面是示例:

"""
There are 80 possible objects that you can detect with the
ObjectDetection class, and they are as seen below.

    person,   bicycle,   car,   motorcycle,   airplane,
    bus,   train,   truck,   boat,   traffic light,   fire hydrant,   stop_sign,
    parking meter,   bench,   bird,   cat,   dog,   horse,   sheep,   cow,   elephant,   bear,   zebra,
    giraffe,   backpack,   umbrella,   handbag,   tie,   suitcase,   frisbee,   skis,   snowboard,
    sports ball,   kite,   baseball bat,   baseball glove,   skateboard,   surfboard,   tennis racket,
    bottle,   wine glass,   cup,   fork,   knife,   spoon,   bowl,   banana,   apple,   sandwich,   orange,
    broccoli,   carrot,   hot dog,   pizza,   donot,   cake,   chair,   couch,   potted plant,   bed,
    dining table,   toilet,   tv,   laptop,   mouse,   remote,   keyboard,   cell phone,   microwave,
    oven,   toaster,   sink,   refrigerator,   book,   clock,   vase,   scissors,   teddy bear,   hair dryer,
    toothbrush.

To detect only some of the objects above, you will need to call the CustomObjects function and set the name of the
object(s) yiu want to detect to through. The rest are False by default. In below example, we detected only chose detect only person and dog.
"""
custom = detector.CustomObjects(person=True, dog=True)

任何帮助将不胜感激。

最佳答案

Python 的语法要求关键字参数是有效的标识符,不允许使用空格。您需要解压一个显式字典。

custom = detector.CustomObjects(**{"cell phone": True, "car": True})

作为演示,接受或拒绝手机完全取决于可调用对象(而不是语言语义问题):

>>> def foo(**kwargs):
...   for k, v in kwargs.items():
...     print("Key: {}".format(k))
...     print("Value: {}".format(v))
...
>>> foo(**{"cell phone": 9})
Key: cell phone
Value: 9
<小时/>

请注意,除了文档之外,actual argument defined by CustomObjectscell_phone,而不是 cellphone。如果传递 cell_phone 的值,则该方法返回的 dict 包含字符串 cell Phone 作为键。

关于python - 如何在函数参数中留出空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54494924/

相关文章:

Python类属性报错AttributeError : can't set attribute

python - 随机使用比较运算符?

python - 输出不平衡时的神经网络回归

python - 训练 Keras 模型会产生多个优化器错误

python - 如何从具有词频的CSV文件生成词云

python-3.x - 在 python 3 文件中运行 python 2 代码

python - 无法为 COPY 创建 docker 镜像失败 : stat/var/lib/docker/tmp/docker-builder error

python-3.x - 将 'r'前缀添加到python变量

python-3.x - 检查列表中的项目是否在列表类型的列中可用

python - 如何为 Windows 64 位 Python 2.7 编译孔雀鱼?