Python 関数の引数の指定を任意にしたい

def post_slack(argStr, **ids): if type(argStr) is str: message = argStr else: # 型がstr以外の場合はjson.dumpsでstrに変換 message = ids["StackId"].replace('/', '%2F').replace(':', '%3A') + json.dumps(argStr) send_data = { "text": message, } send_text = json.dumps(send_data) request = urllib.request.Request( "https://hooks.slack.com/services/xxxxx", data=send_text.encode('utf-8'), method="POST" ) # Slackに送信 with urllib.request.urlopen(request) as response: response_body = response.read().decode('utf-8')

Slackに投稿する関数を作成しました。
引数idsの指定は任意で、指定しなくても動作するようにしたいと思い、可変長引数にしています。idsの型はdict 辞書型です。

動作確認したところ

post_slack("引数1")

の場合は動作しましたが

post_slack("引数2", ids)

と引数を2つ指定した場合、下記のエラーになります。
エラー

"errorMessage": "post_slack() takes 1 positional argument but 2 were given",

どのように修正すれば解消されますでしょうか?

コメントを投稿

0 コメント