首页 > 解决方案 > 如何处理方法需要 1 个位置参数但在功能代码中给出了 2 个?

问题描述

我制作了一个非常小的函数来使用 API 清除 Google 日历。该API通常有效。来自Google Developer pages的参考代码如下:

service.calendars().clear('primary').execute()

我的功能是这样的:

def clear_gcal(service): 
    someCal = '''my_calendar_address''' 
    service.calendars().clear(someCal).execute()
    print("Some Google Cal cleared")

使用有效的服务变量运行它(我也使用它添加到日历中,没有错误,我得到“TypeError:method() 需要 1 个位置参数,但给出了 2 个”。我找到的所有解决方案都在 OOP 中使用 self ,但我的代码只是功能性的(我更喜欢这种方式),那么我该如何处理这个看似很常见的错误类型?谢谢,

Traceback (most recent call last):

File "<ipython-input-87-d7bf7ff34210>", line 1, in <module> runfile('C:/Users/b017646/ExportCal/main.py', wdir='C:/Users/b017646/ExportCal')

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile execfile(filename, namespace)

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/b017646/ExportCal/main.py", line 17, in <module> gcal.clear_gcal(service)

File "C:\Users\b017646\ExportCal\gcal.py", line 48, in clear_gcal service.calendars().clear(deaCal).execute()

TypeError: method() takes 1 positional argument but 2 were given

格雷格斯DK

标签: python-3.xgoogle-calendar-api

解决方案


问题:

您没有calendarId正确提供参数。

解决方案:

你应该这样做:

service.calendars().clear(calendarId='primary').execute()

参考:


推荐阅读