缺少1个必要的位置参数:'self'

时间:2013-12-28 04:30:30

标签: python self

我遇到了这段代码的问题:

class Email_Stuff:
    def Get_From_Email(self):
        self.emailaddr = turtle.textinput("Your Email", "What is your email address?")
    def Get_To_Email(self):
        self.recipaddr = turtle.textinput("Client Email", "What is your client's email address?")
    def Get_Email_Username(self):
        self.EmailUser = turtle.textinput("Your Email Username", "What is your email username?")
    def Get_Email_Password(self):
        self.EmailPass = turtle.textinput("Your Email Password", "What is your email Password?")
    def Send_Email(self):
        import smtplib
        server = smtplib.SMTP('smtp.gmail.com', 587)
        server.login((self.EmailUser),(self.EmailPass))
        message = "Python Test Email"
        server.sendmail(self.emailaddr,self.recipaddr,self.message)

我不确定是什么问题。我有自己应该在哪里,我认为

2 个答案:

答案 0 :(得分:0)

我不确定这是您的错误报告的问题,但在您的代码的最后一行,server.sendmail的一个参数是self.message,但这在任何地方都没有定义。您将message分配在该行的正上方,但不是self.message,它将是该类的成员,而不是函数范围中的变量,这是您现在拥有的。我会将该行更改为:server.sendmail(self.emailaddr,self.recipaddr,message)

答案 1 :(得分:0)

def Send_Email(self):

中的

    message = "Python Test Email"
    server.sendmail(self.emailaddr,self.recipaddr,self.message)

您在函数message中分配了一个局部变量,然后在最后一行使用了self.message。简单的解决方法。

您可以这样做:

    self.message = "Python Test Email" #changed to self.message
    server.sendmail(self.emailaddr,self.recipaddr,self.message)

或者:

    message = "Python Test Email" #changed to self.message
    server.sendmail(self.emailaddr,self.recipaddr,message) #changed to message