简体   繁体   English

如何使用“ self”参数调用方法?

[英]How to call method with “self” parameter?

I need to call a function method complete_login: 我需要调用一个函数方法complete_login:

class VKOAuth2Adapter(OAuth2Adapter):  
    ...

    def complete_login(self, request, app, token, **kwargs):
    ...

It should be really easy, but i cant understand what parameter should be send in "self". 这应该真的很容易,但是我不明白应该在“自我”中发送什么参数。 I know why its there, but how to call such method? 我知道为什么它在那里,但是如何调用这种方法呢?

login = VKOAuth2Adapter.complete_login( ??? , request, app, token)

Thanks. 谢谢。

You need to make an instance of the class: 您需要创建该类的实例

v = VKOAuth2Adapter( whatever parameters it needs, if any )

and then you'll call: 然后您将致电:

v.complete_login(request, app, token)

where v will automatically become the method's self . 其中v将自动成为方法的self

The self parameter is usually not passed explicitly when calling a Python method--it is implicit. 调用Python方法时,通常不会显式传递self参数-它是隐式的。 So you'd do this: 因此,您可以这样做:

vk = VKOAuth2Adapter()
vk.complete_login(req, app, token)

The vk instance will be passed as self . vk实例将作为self传递。 You could write the code this way too: 您也可以这样编写代码:

vk = VKOAuth2Adapter()
VKOAuth2Adapter.complete_login(vk, req, app, token)

But that would not be considered normal practice in Python, so use the first option. 但这在Python中不会被视为正常做法,因此请使用第一个选项。

Note that if you are calling the method from inside another method in the same class, you'd do it this way: 请注意,如果要从同一类的另一个方法内部调用该方法,则可以这样进行:

self.complete_login(req, app, token)

That's because self would be an instance of the class, just as vk was in my previous examples. 这是因为self是该类的实例,就像vk在我之前的示例中一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM