Calling variables from inside functions in Python
Question: I know I have already asked a question like this before but I have made my
code much cleaner and I am still coming up with a problem.

My code goes like this:

    
    
        class Email_Stuff:
            def Get_From_Email():
                #code to open up window and get email address
                emailaddr = #the input
                return emailaddr
            def Get_To_Email():
                #code to open up window and get to email address
                recipaddr = #the input
                return recipaddr
            def Get_Email_Address():
                #code to open up window and get email username
                EmailUser = #the input
                return EmailUser
            def Get_Email_Password():
                #code to open up window and get email password
                EmailPass = #the input
                return EmailPass
            def Send_Email():
                import smtplib
                server = smtplib.SMTP('smtp.gmail.com', 587)
                server.login((EmailUser),(EmailPass))
                message = "Python Test Email"
                server.sendmail(emailaddr,recipaddr,message)
    

I need to get the variables: `emailaddr`, `recipaddr`, `EmailUser`, and
`EmailPass` into the function `Send_Email`. I'm not sure how I could do that
though because when I run this code, it tells me that "the global name isn't
defined".

Any ideas?


Answer: Make emailaddr, recipaddr, EmailUser, and EmailPass become instance variables
by adding prefix "self.".

    
    
    class Email_Stuff():
        def Get_From_Email(self):
            #code to open up window and get email address
            self.emailaddr = #the input
        def Get_To_Email(self):
            #code to open up window and get to email address
            self.recipaddr = #the input
        def Get_Email_Address(self):
            #code to open up window and get email username
            self.EmailUser = #the input
        def Get_Email_Password(self):
            #code to open up window and get email password
            self.EmailPass = #the input
        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)
    
    instance = Email_Stuff()
    instance.Get_From_Email()
    instance.Get_To_Email()
    instance.Get_Email_Address()
    instance.Get_Email_Password()
    instance.Send_Email()
    

BTW, name of methods should be lowercase.

