Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python function takes exactly 2 arguments (1 given)

Tags:

python

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:

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?

like image 951
user1592380 Avatar asked Dec 11 '22 09:12

user1592380


2 Answers

self is usually used for instance methods, passing a pointer of the instance to the function.

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!

like image 147
aIKid Avatar answered Dec 31 '22 02:12

aIKid


You have to invoke the function through a View instance.

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.

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)

you don't have actually passed a object (self) as first param to some_function.

like image 44
Raydel Miranda Avatar answered Dec 31 '22 02:12

Raydel Miranda