Python 函数正好接受 2 个参数(给定 1 个)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20429846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python function takes exactly 2 arguments (1 given)
提问by user61629
I am trying to parse gmail emails. From http://yuji.wordpress.com/2011/06/22/python-imaplib-imap-example-with-gmail/I have the following:
我正在尝试解析 gmail 电子邮件。从http://yuji.wordpress.com/2011/06/22/python-imaplib-imap-example-with-gmail/我有以下内容:
def get_first_text_block(self, email_message_instance):
maintype = email_message_instance.get_content_maintype()
if maintype == 'multipart':
for part in email_message_instance.get_payload():
if part.get_content_maintype() == 'text':
return part.get_payload()
elif maintype == 'text':
return email_message_instance.get_payload()
def hello(request):
....
first = qs[0]
one = first.get_email_object()
print one['To']
out = get_first_text_block(one)
I'm getting:
我越来越:
get_first_text_block() takes exactly 2 arguments (1 given)
What am I doing wrong?
我究竟做错了什么?
采纳答案by Raydel Miranda
You have to invoke the function through a View instance.
您必须通过 View 实例调用该函数。
It's a common error in Django users don't instantiate class Views when writting url patterns. If you have the view "MyView" you must provide an instance MyView() to the url pattern.
这是 Django 用户在编写 url 模式时没有实例化类视图中的一个常见错误。如果您有视图“MyView”,则必须为 url 模式提供一个实例 MyView()。
If you have some class:
如果你有一些课程:
class SomeClass:
def some_function(self, foo): pass
you must call it like this:
你必须这样称呼它:
some_instance = SomeClass()
some_instance.some_function(foo)
if you call it like this:
如果你这样称呼它:
SomeClass.some_function(foo)
SomeClass.some_function(foo)
you don't have actually passed a object (self) as first param to some_function.
您实际上并没有将对象(self)作为第一个参数传递给 some_function。
回答by aIKid
selfis usually used for instance methods, passing a pointer of the instance to the function.
self通常用于实例方法,将实例的指针传递给函数。
Well, we don't seem to need it here. You can just omit it:
好吧,我们这里似乎不需要它。你可以省略它:
def get_first_text_block(email_message_instance):
maintype = email_message_instance.get_content_maintype()
if maintype == 'multipart':
for part in email_message_instance.get_payload():
if part.get_content_maintype() == 'text':
return part.get_payload()
elif maintype == 'text':
return email_message_instance.get_payload()
I don't know where you get this code from, but i hope this works as expected!
我不知道你从哪里得到这个代码,但我希望它能按预期工作!
回答by dano
You've defined get_first_text_block as if it was a instance method by having 'self' as the first parameter. But it's actually just a function. Make the function definition look like this:
通过将“self”作为第一个参数,您已经将 get_first_text_block 定义为一个实例方法。但它实际上只是一个函数。使函数定义如下所示:
def get_first_text_block(email_message_instance):
And it will work.
它会起作用。
回答by Alvaro Fuentes
The selfargument is only used when you define a function as a class method.
So if you want your function to be a global function in your module you should remove it.
Besides in your code I don see why you need a the argument self?
该self参数仅在您将函数定义为类方法时使用。因此,如果您希望函数成为模块中的全局函数,则应将其删除。除了在您的代码中,我不明白您为什么需要参数self?

