首页 > 解决方案 > 如何在函数参数中留出空格?

问题描述

当以下行的两个单词之间有空格时,以下代码如何正确工作以使每个参数等于 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)

任何帮助将非常感激。

标签: pythonpython-3.xpython-2.7

解决方案


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

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

作为一个证明,接受或拒绝完全取决于可调用对象cell phone(而不是语言语义问题):

>>> 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

请注意,除了文档之外,由is定义的实际参数CustomObjectscell_phone不是cell phonecell_phone如果传递了一个值,则该方法返回的值包含作为键dict的字符串。cell phone


推荐阅读