函数参数中如何允许空格?

2024-03-29 08:55:17 发布

您现在位置:Python中文网/ 问答频道 /正文

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

任何帮助都将不胜感激。你知道吗


Tags: andthetotrueonlycustomphonecell
1条回答
网友
1楼 · 发布于 2024-03-29 08:55:17

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 ^{}cell_phone,而不是cell phone。如果传递了cell_phone的值,则方法返回的dict包含作为键的字符串cell phone。你知道吗

相关问题 更多 >