简体   繁体   English

在Python中调用内部函数的变量

[英]Calling variables from inside functions in Python

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 . 我需要将变量: emailaddrrecipaddrEmailUserEmailPass放入函数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? 有任何想法吗?

Make emailaddr, recipaddr, EmailUser, and EmailPass become instance variables by adding prefix "self.". 通过添加前缀“self”,使emailaddr,recipaddr,EmailUser和EmailPass成为实例变量。

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. 顺便说一下,方法的名称应该是小写的。

First, if you want these functions to methods associated with an instance of this class, then each method will need a reference to the instance itself as the first argument, usually designated as self , though you can name it anything you like. 首先,如果您希望这些函数与这个类的实例相关联的方法,那么每个方法都需要引用实例本身作为第一个参数,通常指定为self ,尽管您可以将它命名为任何您喜欢的名称。

For example: 例如:

 def Get_Email_Password(self):
    #code to open up window and get email password
    EmailPass = #the input
    return EmailPass

Next, you have two options to get the values ready for sendmail . 接下来,您有两个选项可以为sendmail准备好值。 You can either call each method from within the Send_Email method and store the returned values for each one. 您可以从Send_Email方法中调用每个方法,并为每个方法存储返回的值。 That would look like this: 这看起来像这样:

def Send_Email(self):
    emailaddr = self.Get_For_Email()
    recipaddr = self.Get_Email_Address()
    ...

Or you can store the values, instead of returning them, as instance variables. 或者,您可以将值存储为实例变量,而不是将其返回。 So, you would have something like this: 所以,你会有这样的事情:

 def Get_Email_Password(self):
    #code to open up window and get email password
    EmailPass = #the input
    self.emailaddr = EmailPass

And then, in your Send_Email method, you would reference the instance variables you have saved: 然后,在Send_Email方法中,您将引用已保存的实例变量:

def Send_Email(self):
    ...
    server.sendmail(self.emailaddr, self.recipaddr, self.message)

How you choose to do it is up to you, but I prefer the first way. 你如何选择这样做取决于你,但我更喜欢第一种方式。 I also suggest you read up on PEP8 我还建议你阅读PEP8

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

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