【发布时间】:2020-09-25 17:59:02
【问题描述】:
我正在使用谷歌课堂 api,函数的参数之一需要回调。
def callback(request_id, response, exception):
if exception is not None:
print 'Error adding user "{0}" to the course course: {1}'.format(
request_id, exception)
else:
print 'User "{0}" added as a student to the course.'.format(
response.get('profile').get('name').get('fullName'))
在我的情况下,该方法在一个类中,并且我知道每个 python 方法都应该采用一个 self 对象,但在这种情况下,这个函数将具有特定的参数,我不确定如何传递 self 对象或者我应该忽略 self ?问题是我计划在函数内部使用 self 。 有什么想法吗?
【问题讨论】:
-
您不需要显式传递
self。但是由于callback没有self作为它的第一个参数,你确定它没有用@staticmethod注解吗? -
你说的是什么方法?您可以将 绑定 方法作为参数 (
somefunc(some_obj.method)) 传递,也可以将方法调用包装在函数中 (somefunc(lambda *args: some_obj.method(*args))),具体取决于您的需要。 -
出于这个确切原因,我试图理解
@static and @class方法。公平地说,我不确定是否真的需要使用这些装饰器。
标签: python self google-classroom python-class