4

Pythonの文法について質問です。
Pythonで()が2連続するのはどういう用法、意味があるのでしょうか。
Amazon alexaのskill開発にあたり、デフォルトのpythonコードに下記のようなコードがあり疑問に思ったため質問いたしました。

return ask_utils.is_request_type("LaunchRequest")(handler_input)

初歩的な質問ですが、ご回答いただけると幸いです。

1 件の回答 1

4

ask_utils.is_request_type は関数を返す高階関数ではありませんか?

https://python.atelierkobato.com/high/#outline__1_2

return ask_utils.is_request_type("LaunchRequest")(handler_input)

は以下を一行で書いたものです。

func = ask_utils.is_request_type("LaunchRequest")
return func(handler_input)

定義を確認してみると:
https://github.com/alexa/alexa-skills-kit-sdk-for-python/blob/240e15f2bedbb03e2fb4fd85f96743bc9e8ff78c/ask-sdk-core/ask_sdk_core/utils/predicate.py#L78-L97

def is_request_type(request_type):
    # type: (str) -> Callable[[HandlerInput], bool]
    """A predicate function returning a boolean, when request type is
    the passed-in type.

    The function can be applied on a
    :py:class:`ask_sdk_core.handler_input.HandlerInput`, to check
    if the input request type is the passed in request type.

    :param request_type: request type to be matched with the input's request
    :type request_type: str
    :return: Predicate function that can be used to check the type of
        the request
    :rtype: Callable[[HandlerInput], bool]
    """
    def can_handle_wrapper(handler_input):
        # type: (HandlerInput) -> bool
        return (handler_input.request_envelope.request.object_type ==
                request_type)
    return can_handle_wrapper

can_handle_wrapper が関数になっています。

1
  • ご回答ありがとうございます。 助かりました、一行での表現を全く見たことなかったものだったので戸惑ってしました。
    – Sou T
    2020年1月30日 7:12

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。