如何在函数参数中留出空间?

时间:2019-02-02 16:17:20

标签: 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)

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

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

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